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"]
1// Javascript Regex Reference
2// /abc/ A sequence of characters
3// /[abc]/ Any character from a set of characters
4// /[^abc]/ Any character not in a set of characters
5// /[0-9]/ Any character in a range of characters
6// /x+/ One or more occurrences of the pattern x
7// /x+?/ One or more occurrences, nongreedy
8// /x*/ Zero or more occurrences
9// /x?/ Zero or one occurrence
10// /x{2,4}/ Two to four occurrences
11// /(abc)/ A group
12// /a|b|c/ Any one of several patterns
13// /\d/ Any digit character
14// /\w/ An alphanumeric character (“word character”)
15// /\s/ Any whitespace character
16// /./ Any character except newlines
17// /\b/ A word boundary
18// /^/ Start of input
19// /$/ End of input
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.