




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of JavaScript regular expressions, including pattern syntax, modifiers, and the RegExp and String APIs. It covers topics such as literal characters, metasyntax characters, character sets, capture groups, lookahead and lookbehind, and greedy and lazy repeat modifiers.
What you will learn
Typology: Cheat Sheet
1 / 8
This page cannot be seen from the preview
Don't miss anything!
Example:
const str = 'Regular Expressions'; /re/.test( str ); false /re/i.test( str ); // matches: 're', 'Re', 'rE', 'RE' true Regex API
const regex = /ab/; const str = 'bbababb'; const noMatchStr = 'c'; regex.exec( str ); // first match is at index 2 [ 0: "ab", index: 2, input: "bbababb" ] regex.exec( noMatchStr ); null regex.test( str ); true regex.test( noMatchStr ); false regex.exec( noMatchStr ); const globalRegex = /ab/g; globalRegex.exec( str ); globalRegex.exec( str ); [ 0: "ab", index: 2, input: "bbababb" ] > globalRegex.exec( str );
/a/.test( 'André' ) // false, because there is no 'a' in the string /a/i.test( 'André') // true: 'A' matches /a/i Literal characters are: all characters except metasyntax characters such as: ., *, ^, $, [, ], (, ), {, }, |, ?, +,
When you need a metasyntax character, place a backslash in front of it. Examples: ., \, [. Whitespaces:
Examples: /.../.test( 'ab' ) // false, we need at least three arbitrary characters /.a/.test( 'a' ) // true,. matches 'a', and a matches '' /^a/.test( 'ba' ) // false, 'ba' does not start with a /^a/.test( 'ab' ) // true, 'ab' starts with a /a$/.test( 'ba' ) // true, 'ba' ends with a /a$/.test( 'ab' ) // false, 'ab' does not end with a /^a$/.test( 'ab' ) // false, 'ab' does not fully match a /^a*$/.test( 'aa' ) // true, 'aa' fully matches a pattern consisting of // a characters only /[ab]/.test( 'b' ) // true, 'b' contains a character that is a // member of the character class [ab]
/a|b/.test( 'b' ) // true, 'b' contains a character that is // either a
or b
/ba?b/.test( 'bb' ) // true, the optional a is not included /ba?b/.test( 'bab') // true, the optional a is included /ba?b/.test( 'bcb') // false, only matches 'bb' or 'bab' /a+/.test( '' ) // false, at least one a
character is needed /a+/.test( 'ba' ) // true, the a
character was found /a{3}/.test('baaab') // true, three consecutive 'a' characters were found /(a|b)c/.test('abc') // true, a b
character is followed by c
/a(?=b)/.test('ab') // true. It matches 'a', because 'a' is followed by 'b' /a(?!b)/.test('ab') // false, because 'a' is not followed by 'b' /\ba/.test('bab') // false, because 'a' is not the first character of a word /\Ba/.test('bab') // true, because 'a' is not the first character of a word /(\d)\1/.test('55') // true. It matches two consecutive digits with the same value
Character sets, character classes
0 "aceff" 1 "ceff" 2 "e" 3 "e" 4 "ff" index 0 input "aceff"
console.table( /^a(b|c(?:d|(e))(f+))$/.exec( 'aceff' ) ) (index) Value 0 "aceff" 1 "ceff" 2 "e" 3 "ff" index 0 input "aceff" Lookahead and Lookbehind
/a(?=b)/.exec( 'ab' ) ["a", index: 0, input: "ab"] /a(?!\d)/.exec( 'ab' ) ["a", index: 0, input: "ab"] > /a(?!\d)/.exec( 'a0' ) null /(?<=a)b/.exec( 'ab' ) ["b", index: 1, input: "ab"] // executed in latest Google Chrome /(?<!a)b/.exec( 'Ab' ) ["b", index: 1, input: "Ab"] // executed in latest Google Chrome
/\bregex\b/.exec( 'This regex expression tests word boundaries.' ) ["regex", index: 5, input: "This regex expression tests word boundaries."] /^regex$/.exec( 'This\nregex\nexpression\ntests\nanchors.' ) null /^regex$/m.exec( 'This\nregex\nexpression\ntests\nanchors.' ) ["regex", index: 5, input: "Thisregexexpressiontestsanchors."] Possessive Repeat Modifiers Attempts a maximal (greedy) match first. The loop does not backtrack though. It either accepts the maximal match, or fails. Possessive repeat modifiers don’t exist in JavaScript. However, there are workarounds. Assuming we don’t have any other capture groups in front of the expression, use
Recommended RegExp library XRegExp^1