简体   繁体   中英

capturing group in regex

I am exploring capturing groups in Regex and I am getting confused about lack of documentation on it. For ex, can anyone tell me difference between two regex:

/(?:madhur)?/

and

/(madhur)?/

As per me, ? in second suggests matching madhur zero or once in the string.

How is the first different from second ?

The first one won't store the capturing group, eg $1 will be empty. The ?: prefix makes it a non capturing group . This is usually done for better performance and un-cluttering of back references.

In the second example, the characters in the capturing group will be stored in the backreference $1 .

Further Reading .

Here's the most obvious example:

"madhur".replace(/(madhur)?/, "$1 ahuja");   // returns "madhur ahuja"
"madhur".replace(/(?:madhur)?/, "$1 ahuja"); // returns "$1 ahuja"

Backreferences are stored in order such that the first match can be recalled with $1 , the second with $2 , etc. If you capture a match (ie (...) instead of (?:...) ), you can use these, and if you don't then there's nothing special. As another example, consider the following:

/(mad)hur/.exec("madhur");   // returns an array ["madhur", "mad"]
/(?:mad)hur/.exec("madhur"); // returns an array ["madhur"]

It doesn't affect the matching at all.

It tells the regex engine

  • not to store the group contents for use (as $1, $2, ...) by the replace() method
  • not to return it in the return array of the exec() method and
  • not to count it as a backreference (\\1, \\2, etc.)

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