JavaScript Regex Syntax

, , …,
Want to master JavaScript in a week? Example based, to the point. Xah JavaScript Tutorial.

Regex Flags

Regex Flags
gglobal. Find all matches. Don't stop after first found.
iignore case
mmultiline; make the regex syntax ^ and $ match any newline beginning/end. (not just the beginning/end of whole string)
ysticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of the lastIndex property.

Special Escapes for Literal Characters

Regex Special Escapes
\0the NUL character (ASCII 0)
\thorizontal tab (common tab char)
\nline feed (unix newline char)
\vvertical tab (rarely used)
\fform feed (often used in emacs as code section break)
\rcarriage return (used in Mac OS Classic as newline)
\xxxa ASCII character of hex code xx. ⁖ /\x61/ matches the letter “a” (ASCII code 97, hex 61)
\uxxxxa Unicode character with hex code xxxx. It must be 4 digits. Add 0 in front if not. ⁖ /\u03b1/ matches “α” (codepoint 945, hex 3b1)
\cXa ASCII control character. ⁖ /\cJ/ matches the unix newline \n.
[\b]A backspace.

〔☛ Unicode: Character Representation, ASCII Character Symbols ␀ ␤ ␠ ␣ ¶ ↩ �

// example of regex matching unicode by hex codepoint
var xx = "alpha α";
console.log(\u03b1/)); // 6
console.log(α/));    // 6. literal Unicode is ok too.

Character Sets, Character Classes

Regex Character Classes
[…]any character between the brackets.
[^…]any char that's not one of the character in the brackets.
.any char, except newline characters: {\n, \r, \u2028, \u2029}.
Regex Character Classes
\wany letter (upper or lower) or digit.
\Wany character that is not \w.
\dany ASCII digit 0 to 9.
\DAny character that's not \d.
\sany Unicode whitespace character.
\Sany character that is not \s.


Regex Character Classes
^beginning of string. If flag /…/g is set, also match beginning of line.
$end of string. If flag /…/g is set, also match end of line.
\bword boundary. For literal backspace, use [\b]
\BNot word boundary.


Regex Repetition Syntax
*Match previous pattern 0 or more times. Same as {0,}.
?Match previous pattern 0 or 1 time. Same as {0,1}.
+Match previous pattern 1 or more times. Same as {1,}.
{n}Match previous pattern exactly n times.
{n,}Match previous pattern n or more times.
{n,m}Match previous pattern n times or up to m times (inclusive).

Note: these will match as far as possible. For non-greedy version, add a ? after them.

Alternate and Conditions

Regex Repetition Syntax
x|yAlternate. Matches either x or y
x(?=y)Matches only if x is followed by y
x(?!y)Matches only if x is not followed by y

Capture Grouping, Back Reference

Regex Repetition Syntax
(…)Capture. Captured group can be later referenced by /n where n is a digit. \1 is the first captured group.
(?:…)Group, but don't captured.
/nThe nth captured group before. \1 is the first captured group.


ECMAScript §15#sec-15.10.1

blog comments powered by Disqus