javascript - Regex capturing : get the whole match in a capturing group -


i can't capture work :

my aim capture in string length of first capture group :

var regex = /(_)?;([\w]+);([\w]+);/; var string = "____;foo;bar;"; var matches = regex.exec(string);  console.log(matches); // outputs  ["_;foo;bar;", "_", "foo", "bar"] 

as can see, matches[1] contains capturing group undescores, gives me matched character, not underscores. expect result :

["_;foo;bar;", "_____", "foo", "bar"] 

is there way achieve regex ? prefer avoid splitting string ; ...

you use pattern this:

/(_*);(\w+);(\w+);/ 

which give you:

["_____;foo;bar;", "_____", "foo", "bar"] 

this pattern match following sequence:

  • zero or more underscores, captured in group 1
  • a semicolon
  • one or more word characters, captured in group 2
  • a semicolon
  • one or more word characters, captured in group 3
  • a semicolon

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -