Sometimes the desired match relies on it's context. This means a simple RegExp will over-find the piece of the String that is of interest, so the solution is to write a capture group
(pattern). The captured data can then be referenced as...
nis the n th capture group (starting from
g, the n+1 th item in a returned
str.matchdiscards captures, use
Say there is a String where all
+ signs need to be replaced with a space, but only if they follow a letter character. This means a simple match would include that letter character and it would also be removed. Capturing it is the solution as it means the matched letter can be preserved.
let str = "aa+b+cc+1+2", re = /([a-z])\+/g; // String replacement str.replace(re, '$1 '); // "aa b cc 1+2" // Function replacement str.replace(re, (m, $1) => $1 + ' '); // "aa b cc 1+2"
Using the form
(?:pattern), these work in a similar way to capture groups, except they do not store the contents of the group after the match.
They can be particularly useful if other data is being captured which you don't want to move the indices of, but need to do some advanced pattern matching such as an OR
let str = "aa+b+cc+1+2", re = /(?:\b|c)([a-z])\+/g; str.replace(re, '$1 '); // "aa+b c 1+2"
If the desired match relies on something which follows it, rather than matching that and capturing it, it is possible to use a look-ahead to test for it but not include it in the match. A positive look-ahead has the form
(?=pattern), a negative look-ahead (where the expression match only happens if the look-ahead pattern did not match) has the form
let str = "aa+b+cc+1+2", re = /\+(?=[a-z])/g; str.replace(re, ' '); // "aa b cc+1+2"