I always forget how to do this:

r = /^[^0-9]+/;
regex = new RegExp('^[^0-9]+', 'g');
/* The 'g' can be used in replace later. */
string = '1a';

me = 'b2';

/* these return false and null, respectively */

string.match(r);
regex.exec(string);

/* this returns true and an Array of matches respectively */

me.match(r);
regex.exec(me);

 

Then how about capture groups – you can’t iterate over them in one go, that’s the problem, you have to do it via exec() which you run multiple times on the same str variable, weirdly enough. The return value is an array with always just two elements: the entire match and the capture group match.

let str = '<h1>Hello, world!</h1> <h1>Hello1, world!</h1> <h1>Hello3, world!</h1>';
 
let reg = /<h1>(.*?)<\/h1>/g;
 
let match;
 
while (match = reg.exec(str)) {
 console.log(match);
}

 

Voila – you get first the 2 elements, “<h1>Hello, world!</h1>” and “Hello, world!” and then the two elements, “<h1>Hello1, world!</h1>” and “Hello1, world!” and so on

Replacing

This is a method on String, not in Regex. Bah.

'abc12ab1'.replace1)new RegExp('\d', 'g', 'N');

This should give you ‘abcNNabN’

References   [ + ]

1. new RegExp('\d', 'g'