Javscript Regex Cheatsheet

I always forget how to do this:

r = /^[^0-9]+/

string = '1a'

me = 'b2'

/* this returns false */


/* this returns true */


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

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