JavaScript Split Function with RegExp Regular Expression (/\s*$/)

问题: it is about splitting at the string end with a regexp as splitter console.log('ab '.split(/s*$/)); outputs : ['ab', ''] but if I remove th...

问题:

it is about splitting at the string end with a regexp as splitter

console.log('ab '.split(/s*$/));

outputs : ['ab', '']

but if I remove the ending space

console.log('ab'.split(/s*$/))

outputs : ['ab']

why the second one does not have a '' in the output ?


回答1:

What happens here is that your regular expression /s*$/ when given the input "ab" is actually equivalent to /$/, since the * quantifier matches between zero and unlimited times.

So this regular expression will only match the empty string at the end of lines in your input, as can be seen with this multiline version:

const input = `abc
def
ghi`;

// this will preserve the `n` new line characters, because we split on the empty `/$/`
console.log(input.split(/s*$/m));

However, the ECMAScript definition of String.split() has a special note regarding the case of splitting with regular expressions that may match such empty string, which reads:

The value of separator may be an empty String, an empty regular expression, or a regular expression that can match an empty String. In this case, separator does not match the empty substring at the beginning or end of the input String, nor does it match the empty substring at the end of the previous separator match.

So this says that the first and last empty strings in our input are discarded (If it were otherwise, "ab".split('') would return ["", "a", "b", ""]).

So at the end what we have can be schematized to

"ab"
   | // should split here
["ab", ""]
   // but we have to discard first and last empty strings
["ab"]

And you can see that the same would happen with /^/ regex

console.log("ab".split(/^/))


回答2:

split converts a string into an array of substrings in a new array. For example-

"ab cd".split(" ")

result: ["ab", "cd"]

it splits where there is a white space " ". but if there is no white space it will create array contains main string. For example-

"abcd".split(" ")

result: ["abcd"]

Now come to the part of regexp.

The s is used to find a whitespace character.

'ab '.split(/s/) results ["ab", ""] and 'ab'.split(/s/) results ["ab"]

First string contains one white space, it seperates the string into two substring, but second string contains no whitespace, so it creates no substring.

The n* matches any string that contains zero or more occurrences of n.

so, s* matches any string that contains zero or more whitespace character.

'ab '.split(/s*/) results ["a", "b", ""] and 'ab'.split(/s*/) results ["a", "b"]

Since First string contains one whitespace and one zero whitespace (total two), it splits the string into three substring. but Second String contains only one zero white space, so it creates two substring.

The n$ quantifier matches any string with n at the end of it.

so s*$ matches string any string that at least one whitespace. Because it will match only at the end of whitespace, if there is no whitespace, there will no end. so, it will not match.

'ab '.split(/s*$/) results ["ab", ""] and 'ab'.split(/s*$/) results ["ab"]

As first string contains one white space, it split the string into two substring, but because of not having any whitespace in second string, it will create no substring.

In short (/s*$/) pattern searching for any whitespace at the end of the whitespace. When you use split(/s*$/), it will only split when the pattern matches.

  • 发表于 2019-07-05 10:52
  • 阅读 ( 162 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除