最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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"]
发布评论

评论列表(0)

  1. 暂无评论