The regexp is not global in the example above, so it only looks for the first match.
Let’s add the
g flag to look for all digits:
let str = "+7(903)-123-45-67"; let reg = /\d/g; alert( str.match(reg) ); // array of matches: 7,9,0,3,1,2,3,4,5,6,7
That was a character class for digits. There are other character classes as well.
Most used are:
\d(“d” is from “digit”)
\s(“s” is from “space”)
\w(“w” is from “word”)
\d\s\w means a digit followed by a space character followed by a wordly character, like
A regexp may contain both regular symbols and character classes.
CSS\d matches a string
CSS with a digit after it:
let str = "CSS4 is cool"; let reg = /CSS\d/ alert( str.match(reg) ); // CSS4
Also we can use many character classes:
alert( "I love HTML5!".match(/\s\w\w\w\w\d/) ); // 'HTML5'
The match (each character class corresponds to one result character):
The word boundary
\b – is a special character class.
It does not denote a character, but rather a boundary between characters.
Java in the string
Hello, Java!, but not in the script
The boundary has “zero width” in a sense that usually a character class means a character in the result (like a wordly or a digit), but not in this case.
The boundary is a test.
When regular expression engine is doing the search, it’s moving along the string in an attempt to find the match. At each string position it tries to find the pattern.
When the pattern contains
\b, it tests that the position in string fits one of the conditions:
\w, from the other side – not
For instance, in the string
Hello, Java! the following positions match
So it matches
\bJava\b, but not
\bHell\b (because there’s no word boundary after
l) and not
Java!\b (because the exclamation sign is not a wordly character, so there’s no word boundary after it).
alert( "Hello, Java!".match(/\bHello\b/) ); // Hello alert( "Hello, Java!".match(/\bJava\b/) ); // Java alert( "Hello, Java!".match(/\bHell\b/) ); // null alert( "Hello, Java!".match(/\bJava!\b/) ); // null
Once again let’s note that
\b makes the searching engine to test for the boundary, so that
Java only when followed by a word boundary, but it does not add a letter to the result.
Usually we use
\b to find standalone English words. So that if we want
"Java" language then
\bJava\b finds exactly a standalone word and ignores it when it’s a part of
Another example: a regexp
\b\d\d\b looks for standalone two-digit numbers. In other words, it requires that before and after
\d\d must be a symbol different from
\w (or beginning/end of the string).
alert( "1 23 456 78".match(/\b\d\d\b/g) ); // 23,78
The word boundary check
\b tests for a boundary between
\w and something else. But
\w means an English letter (or a digit or an underscore), so the test won’t work for other characters (like cyrillic or hieroglyphs).
For every character class there exists a “reverse class”, denoted with the same letter, but uppercased.
The “reverse” means that it matches all other characters, for instance:
\d, for instance a letter.
\s, for instance a letter.
In the beginning of the chapter we saw how to get all digits from the phone
+7(903)-123-45-67. Let’s get a “pure” phone number from the string:
let str = "+7(903)-123-45-67"; alert( str.match(/\d/g).join('') ); // 79031234567
An alternative way would be to find non-digits and remove them from the string:
let str = "+7(903)-123-45-67"; alert( str.replace(/\D/g, "") ); // 79031234567
Please note that regular expressions may include spaces. They are treated like regular characters.
Usually we pay little attention to spaces. For us strings
1 - 5 are nearly identical.
But if a regexp does not take spaces into account, it won’ work.
Let’s try to find digits separated by a dash:
alert( "1 - 5".match(/\d-\d/) ); // null, no match!
Here we fix it by adding spaces into the regexp:
alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, now it works
Of course, spaces are needed only if we look for them. Extra spaces (just like any other extra characters) may prevent a match:
alert( "1-5".match(/\d - \d/) ); // null, because the string 1-5 has no spaces
In other words, in a regular expression all characters matter. Spaces too.
"." is a special character class that matches any character except a newline.
alert( "Z".match(/./) ); // Z
Or in the middle of a regexp:
let reg = /CS.4/; alert( "CSS4".match(reg) ); // CSS4 alert( "CS-4".match(reg) ); // CS-4 alert( "CS 4".match(reg) ); // CS 4 (space is also a character)
Please note that the dot means “any character”, but not the “absense of a character”. There must be a character to match it:
alert( "CS4".match(/CS.4/) ); // null, no match because there's no character for the dot
We covered character classes:
\s– space symbols, tabs, newlines.
\S– all but
\w– English letters, digits, underscore
\W– all but
'.'– any character except a newline.
If we want to search for a character that has a special meaning like a backslash or a dot, then we should escape it with a backslash:
Please note that a regexp may also contain string special characters such as a newline
\n. There’s no conflict with character classes, because other letters are used for them.
The time has a format:
hours:minutes. Both hours and minutes has two digits, like
Make a regexp to find time in the string:
Breakfast at 09:00 in the room 123:456.
P.S. In this task there’s no need to check time correctness yet, so
25:99 can also be a valid result.
P.P.S. The regexp shouldn’t match
alert( "Breakfast at 09:00 in the room 123:456.".match( /\b\d\d:\d\d\b/ ) ); // 09:00