regex - In JavaScript, what does the notation s*;s* mean? - Stack Overflow

In the following example, what does the notation s*;s* mean? It is used as the separator in the str

In the following example, what does the notation /\s*;\s*/ mean? It is used as the separator in the string.split() mand.

var names = 'Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ';

console.log(names);

var re = /\s*;\s*/;

var nameList = names.split(re);

console.log(nameList);

In the following example, what does the notation /\s*;\s*/ mean? It is used as the separator in the string.split() mand.

var names = 'Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ';

console.log(names);

var re = /\s*;\s*/;

var nameList = names.split(re);

console.log(nameList);
Share Improve this question edited Dec 13, 2015 at 19:40 j08691 208k32 gold badges269 silver badges280 bronze badges asked Dec 13, 2015 at 19:39 SegwayintoSegwayinto 1271 gold badge1 silver badge3 bronze badges 3
  • 1 It’s a regular expression. – Ry- Commented Dec 13, 2015 at 19:41
  • 1 \s means whitespace * means 0 or more times so this will split at any ; preceded or followed by any amount of whitespace – depperm Commented Dec 13, 2015 at 19:42
  • Yes, it's a separator in split(). It will split the string after match and create and array out of it. – user2705585 Commented Dec 13, 2015 at 19:56
Add a ment  | 

4 Answers 4

Reset to default 3

/\s*;\s*/ is a regular expression and is often used for pattern matching. Your regex (\s*;\s*) translates to:

  • \s matches any white space character [\r\n\t\f]

  • * means apply the previous match between zero and unlimited times, as many times as possible, giving back as needed [greedy]

  • ; matches the ; character literally

  • \s matches any white space character [\r\n\t\f] (same as bullet #1)

  • * means apply the previous match between zero and unlimited times, as many times as possible, giving back as needed [greedy] (same as bullet #2)

The reason a regular expression is used here rather than a simple string argument to .split is because the original string has varying separators between the elements - different bations of spaces and a semi-colon. There isn't a simple string that could be passed that would find them all - you could use just ;, but that would leave spaces at the start and/or end of some elements of the array. The regular expression ensures we match all the variations.

With JavaScript's .split(separator) method, you are making an array from a string. separator is either a simple string or - more plex - regular expression.

In your example, the regular expression provided is /\s*;\s*/. Front and back slashes are just the delimiters saying that it's a regular expression. \s*;\s* is your pattern, meaning: any number of whitespace characters (spaces, tabs), followed by a semicolon, followed by any number of whitespace characters. It's a more fancy version of "my string".split(";"), because it trims the whitespace before and after the semicolon.

Example:

var test1 = "dog;cat;fish".split(";");
// test1 is ["dog", "cat", "fish"]

var test2 = "dog;cat;fish".split(/\s*;\s*/);
// test2 is ["dog", "cat", "fish"]

As long as there are no white characters around the semicolon, those two return exactly the same result. But:

var test3 = "dog ; cat; fish".split(";");
// test3 is ["dog ", " cat", " fish"]

var test4 = "dog ; cat; fish".split(/\s*;\s*/);
// test4 is ["dog", "cat", "fish"]

This time, results of test4 are far more sophisticated, because no spaces are involved.

In regular expressions \s meta-character stands for whitespace character.

A whitespace character can be:

A space character
A tab character
A carriage return character
A new line character
A vertical tab character
A form feed character

* stands for all.
; stands for colon.

The expression here \s*;\s* means that any whitespace followed or preceded by ; will be split into an array using spilit() method of JavaScript.

Roughly saying, it splits the input string by ; omitting any amount of spaces all around this ;.


var input = "One ;    Two   ;Three   ";
input.split(";")        // returns ["One ", "    Two   ", "Three   "]
input.split(/\s*;\s*/)  // returns ["One", "Two", "Three"]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742420884a4440655.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信