Regular expressions provide a way for really powerful substring search and replace.
Let’s say we’ve got to find a digit in a string. Not the given digit, but any digit, so it should find
Only 1 and
Give me a 5.
Substring matching can be used to search for all digits from 0 to 9 in a loop. But the regexp matching handles the case gracefully.
There are characters which have special use in regexps:
[ \ ^ $ . | ? * + ( ).
They are special, because they are used to enhance regexp searching abilities. Don’t try to remember the list. You will find them easy to remember after we cover them.
To use a special character as a regular symbol, it must be escaped. Or, in other words, prepended with a backslash.
For example, we need to find the dot
'.'. In a regexp, it is a special symbol meaning any character excepts a newline.
Several characters or character classes may be grouped in square brackets
[...] to search for any of them.
[eao] means any of characters ‘a’, ‘e’, or ‘o’. That’s a single char from the list.
search, match and
Say, we’ve got to find a 3-digit number. With
\d that’s simple:
showMatch( "I'm 100 years old", /\d\d\d/ ) // 100
But let’s go a step further. What if we want to search for 5-digit numbers. Should we repeat
\d 5 times:
Luckily, there is a better way.
Alternation is denoted by the vertical line
'|'. It allows to choose between multiple variants.
A part of the regular expression can be grouped together in brackets
Quantifiers are applied to whole group instead of just one char.
There are short quantifiers
'+', '*' and
'?', which are used very widely.
Basically, they are convenient shortcuts for numeric quantifiers:
Another position check is a word boundary
\b. It doesn’t match a character, but matches in situations when a wordly character follows a non-wordly or vice versa. A “non-wordly” may also be text start or end.
Here you found tasks which help in understanding regexp construction principles.