var regex = /^(\([0-9]{3}\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$/;
RegExp("pattern")
constructor method/pattern/
/"dontdoit"/
/doit/
var phrase = 'I am a fan of AnnieCannons.';
/** Uses RegExp() Constructor **/
var regex = new RegExp("an");
/** Uses literal value **/
var shorthand = /an/;
var flags = /an/gi;
console.log(phrase.match(shorthand));
/** "an", index: 8, input: "I am a fan of AnnieCannons."] **/
console.log(phrase.match(flags));
/** prints ["an", "An", "an"] **/
Character classes
\g
- Global, find ever instance\i
- Ignore case\w
- match word\d
- search for number, digit\D
- omit digit\W
- omit words\s
- match any whitespace character\S
- omit any whitespace character[abc]
- [range]Match any a,b, or c[^abc]
- ^ symbol, do not find a, b, or c[a-zA-z0-9]
- Match any letter or digit
var phrase = 'I am a fan of AnnieCannons.'
var first = /[an]/g
console.log(phrase.match(first));
/** prints ["a", "a", "a", "n", "n", "n", "a", "n", "n", "n"] **/
var second = /[an]/gi
console.log(phrase.match(second));
/** prints ["a", "a", "a", "n", "A", "n", "n", "a", "n", "n", "n"] **/
var third = /[an]/
console.log(phrase.match(third));
/** print ["a", index: 2, input: "I am a fan of AnnieCannons."] **/
var fourth = /\W[an]/ /** Find any a character that does not follow a character (whitespace).
console.log(phrase.match(fourth));
/** prints [" a", " a", " A"] **/
var fifth = /\s/g ** Match any whitespace in our sequence.
console.log(phrase.match(phrase));
/** prints [" ", " ", " ", " ", " "] (All whitespace in phrase variable)**/
/{min,max}/
/\d{3}/
/\d{1,2}/
/\d*/
/\d+/
/\d?/
var num = '123 234 345';
/** Match 1 to 2 digits */
var one = /\d{1,2}/
/** Matches '12' */
/** Matches '123 ' */
var two = /\d{1,2}3./
/** Match 1 to 2 digits preceeding a 3 and any character following */
/** Match 1 to 2 digits preceeding a 3 and any character following */
var two = /\d{1,2}3./
/** Matches '123 ' */
/**Match 1 to 2 digits that proceed a three, proceeded by any character */
var three = /\d{1,2}3.+/
/** Matches entire string '123 234 345**/
/** Find a 3 in between a digit */
var four = \d3\d/
/** Matches '234' */
/** Match 0 or 1 '23' */
var five = /\23?/
/** Matches FIRST '23', 0 or 1 */
()
var regex = /1(23)/
var regex = /1(23)/
var regex = /\d+(2|5)/g
var num = '123.234.3456';
var one = /(23)/g
/** Matches '23', '23' */
/** Match any character before a 2 or three */
var three = /.(2|3)/
/** Matches '12', '.2', '.3' */
/** Match any . symbol before a 2 or three */
var three = /\.(2|3)/
/** Matches '.2' , '.3' */
/** Match any digit proceeded by 2 or 5 */
var four = /\d(2|5)/
/** Matches '12', '45' */
/** Match any digit proceeded by zero or one instances of 2 or 5 */
var five = /\d(?=2|5)/g
/** Matches '1', '4' */
Thank you for your attention!