Back to Home

Character classes

pattern:\d (“d” is from “digit”)

A digit: a character from 0 to 9. pattern:\s (“s” is from “space”)

A space symbol: includes spaces, tabs \ , newlines
and few other rare characters, such as \v, \f and
. pattern:\w (“w” is from “word”)

A “wordly” character: either a letter of Latin alphabet or a digit or an underscore _. Non-Latin letters (like cyrillic or hindi) do not belong to pattern:\w. For instance, pattern:\d\s\w means a “digit” followed by a “space character” followed by a “wordly character”, such as match:1 a. A regexp may contain both regular symbols and character classes. For instance, pattern:CSS\d matches a string match:CSS with a digit after it: Also we can use many character classes: The match (each regexp character class has the corresponding result character):

Inverse classes

For every character class there exists an “inverse class”, denoted with the same letter, but uppercased. The “inverse” means that it matches all other characters, for instance: pattern:\D

Non-digit: any character except pattern:\d, for instance a letter. pattern:\S

Non-space: any character except pattern:\s, for instance a letter. pattern:\W

Non-wordly character: anything but pattern:\w, e.g a non-latin letter or a space. In the beginning of the chapter we saw how to make a number-only phone number from a string like subject:+7(903)-123-45-67: find all digits and join them. An alternative, shorter way is to find non-digits pattern:\D and remove them from the string:

A dot is “any character”

A dot pattern:. is a special character class that matches “any character except a newline”. For instance: Or in the middle of a regexp: Please note that a dot means “any character”, but not the “absence of a character”. There must be a character to match it:

Dot as literally any character with “s” flag

By default, a dot doesn’t match the newline character
. For instance, the regexp pattern:A.B matches match:A, and then match:B with any character between them, except a newline
: There are many situations when we’d like a dot to mean literally “any character”, newline included. That’s what flag pattern:s does. If a regexp has it, then a dot pattern:. matches literally any character: alert( “A
B”.match(/A[\s\S]B/) ); // A
B (match!) alert( “1 - 5”.match(/\d-\d/) ); // null, no match! alert( “1 - 5”.match(/\d - \d/) ); // 1 - 5, now it works // or we can use \s class: alert( “1 - 5”.match(/\d\s-\s\d/) ); // 1 - 5, also works

Summary

There exist following character classes:

let str = "+7(903)-123-45-67";

let regexp = /\d/;

alert( str.match(regexp) ); // 7
Example:

Follow the lesson from Microsoft Web-Dev-For-Beginners course

Tags: web,development