MENU
Special Characters
Character Sets:
. any character except newline or line terminator
[fdrw] any character within
[^fdrw] any character not within
[0-9] any digit from 0 to 9
[A-Z] any uppercase letter
[a-d] a, b, c, or d
[A-z] any letter
car|bus|taxi
“car”,”bus”, or “taxi”
var s = "abcdefghij";
console.log(s.search(/..ef./));
console.log(s.search(/ABC|bbb|de[H-z]/));
console.log(s.search(/[ABC|bbb|dee-z]/));
console.log(s.search(/[^0-9a-z]/));Alphanumeric Characters and Spaces
\w any letter, digit or _ ([A-Za-z0-9_])
\W any character which is not a letter, digit or _ ([^A-Za-z0-9_])
\d any digit ([0-9])
\D any non-digit ([^0-9])
\s any whitespace character
\S any non-whitespace character
var s = " a1b2c3@@@";
console.log(s.search(/\w\W/));
console.log(s.search(/\d\D/));
console.log(s.search(/\s\S/));Boundaries:
\b a word boundary
\B not a word boundary
^c with c at the beginning
c$ with c at the end
var s = "Hello World";
console.log(s.search(/\b/));
console.log(s.search(/o\b/));
console.log(s.search(/\B\B\B/));
console.log(s.search(/^.../));
console.log(s.search(/...$/));Special Characters:
\0 NUL character
\n newline character
\f form feed character
\r carriage return character
\t tab character
\v vertical tab character
[\b] a backspace
\cX a control character in a string, X is from A-Z
// nicely alligned
console.log("3\t3");
console.log("20\t20");To escape other special characters, add a \ in front of: . \ + * ? [ ] ^ $ ( ) { } = ! < > | : -
var s = "Hello World!\n";
console.log(s.replace(/[\n\!\[\]]/g, ""));Code Points:
\xxx character specified by the octal number xxx
\xdd character specified by the hex number dd
\uxxxx UTF16 character with hex number xxxx
\u{xxxx} a character only when 'u' flag is set
\u{xxxxx} a character only when 'u' flag is set
var s = "Hello你好";
console.log(s.search(/[\u00FF-\uFFFF]/));\p{property}
Unicode property escapes allow for matching characters based on their Unicode properties. A character is described by several properties which are either binary ("boolean-like") or non-binary. For instance, Unicode property escapes can be used to match emojis, punctuations, letters (even letters from specific languages or scripts), etc.
const sentence = 'A ticket to 大阪 costs ¥2000 👌.';
const regexpEmojiPresentation = /\p{Emoji_Presentation}/gu;
console.log(sentence.match(regexpEmojiPresentation));
const regexpNonLatin = /\P{Script_Extensions=Latin}+/gu;
console.log(sentence.match(regexpNonLatin));
const regexpCurrencyOrPunctuation = /\p{Sc}|\p{P}/gu;
console.log(sentence.match(regexpCurrencyOrPunctuation));Refer to https://www.unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txtfor a list of available Unicode properties.
Quantifiers:
c+ one or more c's
c* zero or more c's
c? zero or one c
c{n} a sequence of n c's
c{n,m} a sequence of n to m c's
c{n,} a sequence of at least n c's
To match the smallest possible set, add a ? to the right of any of the above.
var s = "a-bb-ccc-dddd";
console.log(s.replace(/.*/g, "x"));
console.log(s.replace(/.*?/g, "x"));
console.log(s.replace(/.+/g, "x"));
console.log(s.replace(/.+?/g, "x"));
console.log(s.replace(/.?/g, "x"));
console.log(s.replace(/.??/g, "x"));
console.log(s.replace(/[^\-]{3}/g, "x"));
console.log(s.replace(/[^\-]{3,}/g, "x"));
console.log(s.replace(/[^\-]{3,}?/g, "x"));Assertions:
(?=s) followed by s (excluding s)
(?!s) not followed by s (excluding s)
(?<=s) preceded by s (excluding s)
(?<!s) not preceded by s (excluding s)
var s = "abc";
console.log(s.replace(/ab/, "x"));
console.log(s.replace(/a(?=b)/, "x"));
console.log(s.replace(/a(!=b)/, "x"));
console.log(s.replace(/(?<=a)b/, "x"));
console.log(s.replace(/(?<!a)b/, "x"));