Special Characters

Character Sets: Character Sets: 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 lettercar|bus|taxi“car”,”bus”, or “taxi”


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var s = "abcdefghij";
    console.log(s.search(/..ef./));     // 2
   console.log(s.search(/ABC|bbb|de[H-z]/)); // 3
   console.log(s.search(/[ABC|bbb|dee-z]/)); // 1
   console.log(s.search(/[^0-9a-z]/)); // -1

</script></body><html>

Alphanumeric Characters and Spaces Alphanumeric Characters and Spaces 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


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var s = " a1b2c3@@@";
    console.log(s.search(/\w\W/)); // 6
   console.log(s.search(/\d\D/)); // 2
   console.log(s.search(/\s\S/)); // 0

</script></body><html>

Boundaries: Boundaries: Boundaries:

\b a word boundary\Bnot a word boundary^c with c at the beginning

c$ with c at the end


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var s = "Hello World";
    console.log(s.search(/\b/));        // 0
   console.log(s.search(/o\b/));      // 4
   console.log(s.search(/\B\B\B/)); // 1
   console.log(s.search(/^.../));     // 0
   console.log(s.search(/...$/));      // 8

</script></body><html>

Special Characters: Special Characters: 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

The tab character can be used to nicely aligned columns across multiple lines.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

// nicely allignedconsole.log("3\t3");      // 3
   3console.log("20\t20");  // 10
  10

</script></body><html>

To escape other special characters, add a \ in front of: . \ + * ? [ ] ^ $ ( ) { } = ! < > | : -


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var s = "Hello World!\n";
    console.log(s.replace(/[\n\!\[\]]/g,"")); // Hello World

</script></body><html>

Code Points: Code Points: Code Points:

\xxx character specified by the octal number xxx\xdd character specified by the hex number dd\uxxxx UTF16 character withhex number xxxx\u{xxxx} a character only when 'u' flag is set\u{xxxxx} a character only when 'u' flag is set


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var s = "Hello你好";
    console.log(s.search(/[\u00FF-\uFFFF]/)); // 5

</script></body><html>

\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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

const sentence = 'A ticket to 大阪 costs ¥2000 👌.';const regexpEmojiPresentation =
                                              /\p{Emoji_Presentation}/gu;console.log(sentence.match(regexpEmojiPresentation));// expected output: Array ["👌"]const regexpNonLatin = /\P{Script_Extensions=Latin}+/gu;console.log(sentence.match(regexpNonLatin));// expected output: Array [" ", " ", " 大阪 ", " ¥2000 👌."]const regexpCurrencyOrPunctuation = /\p{Sc}|\p{P}/gu;console.log(sentence.match(regexpCurrencyOrPunctuation));// expected output: Array ["¥", "."]

</script></body><html>

Refer to https://www.unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txtfor a list of available Unicode properties.

Quantifiers: Quantifiers: Quantifiers:

c+ one or more c'sc* zero or more c'sc? zero or one cc{n} a sequence of n c'sc{n,m} a sequence of n to m c'sc{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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var s = "a-bb-ccc-dddd";
    console.log(s.replace(/.*/g,"x"));  // xx
   console.log(s.replace(/.*?/g,"x"));                                       // xax-xbxbx-xcxcxcx-xdxdxdxdx
   console.log(s.replace(/.+/g,"x"));  // x
   console.log(s.replace(/.+?/g,"x")); // xxxxxxxxxxxxx
   console.log(s.replace(/.?/g,"x"));  // xxxxxxxxxxxxxx
   console.log(s.replace(/.??/g,"x"));                                        // xax-xbxbx-xcxcxcx-xdxdxdxdx
   console.log(s.replace(/[^\-]{3}/g,"x"));   // a-bb-x-xd
   console.log(s.replace(/[^\-]{3,}/g,"x"));  // a-bb-x-x
   console.log(s.replace(/[^\-]{3,}?/g,"x")); // a-bb-x-xd

</script></body><html>

Assertions: Assertions: 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)


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var s = "abc"; console.log(s.replace(/ab/,"x"));          // xcconsole.log(s.replace(/a(?=b)/,"x"));    // xbcconsole.log(s.replace(/a(!=b)/,"x"));    // abcconsole.log(s.replace(/(?<=a)b/,"x")); // axcconsole.log(s.replace(/(?<!a)b/,"x"));  // abc

</script></body><html>