1//Declare Reg using slash
2let reg = /abc/
3//Declare using class, useful for buil a RegExp from a variable
4reg = new RegExp('abc')
5
6//Option you must know: i -> Not case sensitive, g -> match all the string
7let str = 'Abc abc abc'
8str.match(/abc/) //Array(1) ["abc"] match only the first and return
9str.match(/abc/g) //Array(2) ["abc","abc"] match all
10str.match(/abc/i) //Array(1) ["Abc"] not case sensitive
11str.match(/abc/ig) //Array(3) ["Abc","abc","abc"]
12//the equivalent with new RegExp is
13str.match('abc', 'ig') //Array(3) ["Abc","abc","abc"]
1var r = /^a$/
2
3function matchExact(r, str) {
4 var match = str.match(r);
5 return match && str === match[0];
6}
1const str = 'For more information, see Chapter 3.4.5.1';
2const re = /see (chapter \d+(\.\d)*)/i;
3const found = str.match(re);
4
5console.log(found);
6
7// logs [ 'see Chapter 3.4.5.1',
8// 'Chapter 3.4.5.1',
9// '.1',
10// index: 22,
11// input: 'For more information, see Chapter 3.4.5.1' ]
12
13// 'see Chapter 3.4.5.1' is the whole match.
14// 'Chapter 3.4.5.1' was captured by '(chapter \d+(\.\d)*)'.
15// '.1' was the last value captured by '(\.\d)'.
16// The 'index' property (22) is the zero-based index of the whole match.
17// The 'input' property is the original string that was parsed.