/pattern/modifiers; // syntax
/abc/ig // match any a, b, or c
Regular expressions are patterns used to match character combinations in strings.
(a|b) // a or b
(...) // Capture Group, subpatterns
(?...) // Passive (non-capturing) group
[abc] // Range (a, b, or c)
[^abc] // Not (a, b, or c)
[a-d] // Lower case letter from a to d
[A-D] // Upper case letter from A to D
[0-3] // Digit from 0 to 3
To target different groups and ranges.
^ // match the beginning of input. Different meaning when used at the start of a character class.
$ // match the end of input. /t$/, matches t in "eat".
\b // matches the beginning or end of a word. /\bLO/ LO at the beginning of a word, /LO\b/ at the end of a word.
\B // matches a non-boundary word. Opposite of above. /\Bon/ "on" in "noon", /ye\B/ "ye" in "yesterday".
x(?=y) // Lookahead Assertion, returns just "x" if there is a "y" ahead in the string
x(?!y) // Negative Lookahead Assertion, is there isn't a "y" ahead, returns just "x"
(?<=y)x // LookBehind Assertion, returns "x" if there is a "y" behind
(?<!y)x // Negative LookBehind Assertion, returns "x" if there is no "y" behind
When a match is possible in some way.
const str = 'Dog person';
const regex = /dog/ig;
console.log(str.replace(regex, 'Cat')); // 'Cat person'
Can use regex patterns to replace words.
/pattern/g // global match
/pattern/i // case-insensitive
/pattern/m // multiple lines, template literals
/pattern/s // treat as single line
Add modifications to regular expressions.
/hu./ // matches hug, hue, hum
[^0-9aeiou] // negated character set, do not match
/caboose$/ // match the end of a string
/t[a-z]*i/ // after t match zero or more of any letter, greedy match, try to match much as possible
/<.*?>/ // match anything between the html tags and stops there, lazy matching
x*? // * - quantifier is greedy by default
x+? // + - quantifier is greedy by default
x?? // ? - quantifier makes it non-greedy, makes it stop as soon as a match is found
x{n}? // match number of "x"s and stop
x{n,}? // match at least number of "x"s and stop
x{n,m}? // match a range of "x"s and stop
Some patterns used with regex.
const str = "hi world!";
const result = /^hi/.test(str);
console.log(result); // true
To test a pattern against a string. Returns either true or false based on if match found.
const str = 'This is a sentence.';
// Any character that is not a word character or whitespace
const regex = /[^\w\s]/g;
console.log(str.search(regex)); // 18
console.log(str[str.search(regex)]); // "."
Use regex to get the index of matched pattern.
. // wildcard, match any character
\d // match any number
\D // match any non-number
\w // match any alphanumeric character [A-Za-z0-9_]
\W // match any non-alphabetical character
\s // match whitespace characters
\S // match any non-whitespace characters
Character classes are sets of characters.
x* // matches "x" 0 or more times, /bo*/, matches b and boo
x+ // matches "x" 1 or more times, equivalent to {1,}
x? // matches "x" 0 or 1 times, /favou?rite/, "favorite" or "favourite"
x{n} // "n" a positive integer, matches exactly "n" occurrences a{2}, aa
x{n,} // matches at least the "n" of occurrences a{2,} aaaaaaa works
x{n,m} // creating a range of occurrences a{1,3}, 1 to 3 a's work
Quantifiers indicate numbers of characters or expressions to match.
const paragraph = 'The dog is angry. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
console.log(found); // Expected output: Array ["T", "I"]
Returns an array of matches.
let str = "Wow a sentence";
let result = /e/.exec(str);
console.log(result); // ["e"]
Executes a search for a match in a specified string and returns a result array, or null.
const string = "How is $everything g$oing?"
const breakpoint = /\$e|\$o/
const splitted = string.split(breakpoint)
console.log(splitted); // ["How is ","verything g","ing?"]
Use regex with split, to split where patterns match.