REGEX Javascript 中两个字符之间的 "no-character space"

The "no-character space" between two characters in REGEX Javascript

因为我们可以从正则表达式中拆分数组,所以我试着理解这个:

console.log( 'hello'.split(/([a-z])/g) );
// returns ['', 'h', '', 'e', '', 'l', '', 'l', '', 'o', '']

return应该是['h','e','l','l','o']

我如何在正则表达式中使用这个“无字符”以及它在计算机科学中代表什么?

我找到这个:Non-breaking space 我试过了:

let carac = String.fromCharCode(parseInt('202F', 16));
'hello'.split(carac);

但是不行。

console.log( 'hello'.split(/([a-z])/g) );
// returns ['', 'h', '', 'e', '', 'l', '', 'l', '', 'o', '']

The return should be [ 'h', 'e', 'l', 'l', 'o']

不,不应该。首先,您选择使用非常具体的语法 (source):

If separator is a regular expression that contains capturing parentheses ( ), matched results are included in the array.

因此您将同时获得结果(空字符串)和分隔符(字母)。如果您省略捕获括号,您只会得到结果:

console.log( 'hello'.split(/[a-z]/g) );

获得 [ 'h', 'e', 'l', 'l', 'o'] 意味着您仅获得分隔符。没有定义这样的语法。要获得这些结果,您需要使用适当的分隔符,即“none”(由空字符串表示):

console.log( 'hello'.split('') );

最后但同样重要的是,non-breaking 空格是字符串可以包含的常规字符,例如 k,它们的主要特点是它们是无形的。它们并不像您想象的那样抽象。