简体   繁体   中英

Capturing parentheses - /(\d)/ ? or /\s*;\s*/?

I am reading about split and below is a variable looking at the string values. However I do not understand what the symbols are looking for.

According to the page: If separator contains capturing parentheses, matched results are returned in the array.

    var myString = 'Hello 1 word. Sentence number 2.';
    var splits = myString.split(/(\d)/);

    console.log(splits);

    // Results 
    [ "Hello ", "1", " word. Sentence number ", "2", "." ]

My question is, what is happening here? Parentheses "(" or ")" is not part of the string. Why is space or "." separated for some and not the other?

Another one is /\\s*;\\s*

States it removes semi-colon before and after if there are 0 or more space. Does this mean /\\s* mean it looks for a space and remove and ';' in this case is the separator?

  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);

  // Results
  ["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand "]

If so why doesn't /\\s*^\\s*/ remobe space before and after ^ symbol if my string looked like this.

  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);

I would like to know what the symbols mean and why they are in certain order. Thanks you.

It seems you got your examples from here .

First let's look at this one /(\\d)/ . Working inside out, recognize that \\d escapes all digits. Now, from the article, wrapping the parentheses around the escape tells the split method to keep the delimiter (which in this case is any digit) in the returned array. Notice that without the parentheses, the returned array wouldn't have numeric elements (as strings of course). Lastly, it is wrapped in slashes ( // ) to create a regular expression . Basically this case says: split the string by digits and keep the digits in the returned array.

The second case /\\s*;\\s* is a little more complicated and will take some understanding of regular expressions. First note that \\s escapes a space. In regular expressions, a character c followed by a * says 'look for 0 or more of c , in consecutive order'. So this regular expression matches strings like ' ; ' ' ; ' , ';' , etc (I added the single quotes to show the spaces). Note that in this case, we don't have parentheses, so the semicolons will be excluded from the returned array.

If you're still stuck, I'd suggest reading about regular expressions and practice writing them. This website is great, just be be weary of the fact that regular expressions on that site may be slightly different than those used in javascript in terms of syntax.

The 1st example below splits the input string at any digit, keeping the delimiter (ie the digit) in the final array.

The 2nd example below shows that leaving the parentheses out still splits the array at any digit, but those digit delimiters are not included in the final array.

The 3rd example below splits the input string any time the following pattern is encountered: as many consecutive spaces as possible (including none) immediately followed by a semi-colon immediately followed by as many consecutive spaces as possible (including none).

The 4th example below shows that you can indeed split a similar input string as in the 3rd example but with "^" replacing ";". However, because the "^" by itself means "the start of the string" you have to tell JavaScript to find the actual "^" by putting a backslash (ie a special indicator designated for this purpose) right in front of it, ie "\\^".

 const show = (msg) => {console.log(JSON.stringify(msg));}; var myString = 'Hello 1 word. Sentence number 2.'; var splits1 = myString.split(/(\\d)/); show(splits1); var splits2 = myString.split(/\\d/); show(splits2); var names1 = 'Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand '; var nameList1 = names1.split(/\\s*;\\s*/); show(nameList1); var names2 = 'Harry Trump ^Fred Barney^ Helen Rigby ^ Bill Abel ^Chris Hand '; var nameList2 = names2.split(/\\s*\\^\\s*/); show(nameList2); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM