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
1// \d Any digit character
2// \w An alphanumeric character (“word character”)
3// \s Any whitespace character (space, tab, newline, and similar)
4// \D A character that is not a digit
5// \W A nonalphanumeric character
6// \S A nonwhitespace character
7// . Any character except for newline
8// /abc/ A sequence of characters
9// /[abc]/ Any character from a set of characters
10// /[^abc]/ Any character not in a set of characters
11// /[0-9]/ Any character in a range of characters
12// /x+/ One or more occurrences of the pattern x
13// /x+?/ One or more occurrences, nongreedy
14// /x*/ Zero or more occurrences
15// /x?/ Zero or one occurrence
16// /x{2,4}/ Two to four occurrences
17// /(abc)/ A group
18// /a|b|c/ Any one of several patterns
19// /\d/ Any digit character
20// /\w/ An alphanumeric character (“word character”)
21// /\s/ Any whitespace character
22// /./ Any character except newlines
23// /\b/ A word boundary
24// /^/ Start of input
25// /$/ End of input
1'string'.match(/regex/);
2 console.log('The 1quick5'.match(/[0-9]/g)); // ['1', '5']
3
4/regex/.test('string');
5console.log(new RegExp('foo*').test('table football')); //true
6
7
1//Adding '/' around regex
2var regex = /\s/g;
3//or using RegExp
4var regex = new RegExp("\s", "g");