Range of Characters and More Regular Expressions in JavaScript
Welcome to the second part of a five-part series on regular expressions in JavaScript. In the first part we explained why you would want to be familiar with regular expressions and began describing them. In this part we'll pick up where we left off, covering special characters, combining with other characters, character classes, and more.
Range of Characters and More Regular Expressions in JavaScript - Some Special Characters (Page 2 of 4 )
The vertical tab
If you want a vertical tab to appear in text you should type “v” in the text. Consider the following:
var availablestring = "vThis is a new section and it continues as a paragraph.";
var re = /v/;
Note the ‘v’ for a vertical tab at the beginning of the availableString.
With the above variables, the following expression should return true in a conditional (if statement).
re.test(availableString)
However, browsers have limitations, so your browser may not handle the vertical tab as it should. It may ignore the vertical tab.
The NUL character
The NUL character in a string is written as "." You have a back slash followed by a zero (not an O). When the string is displayed on the screen, the NUL character should not appear. Consider the following:
var availablestring = "The NUL character is at the end of this phrase ";
var re = //;
With the above variables, the following expression should return true in a conditional (if statement).
re.test(availableString)
The Control Characters
The notation for matching a control character is
cX
where X is a letter from A to Z.
If you only want to match a control character (not associated with other characters), the literal text expression for the regexp is:
/cX/
The Backspace Character
The notation for matching a Backspace character is
[b]
If you only want to match a backspace character, the literal text expression for the regexp is:
/[b]/
Hexadecimal Numbers
In programming, some hexadecimal numbers are written as:
xhh e.g xBF
Other hexadecimal numbers are written as:
xhhhh e.g. xAF7B
I will not give you further details about hexadecimal numbers; just know that you will find many examples like those above.
The notation for matching hexadecimal numbers is
xhh or xhhhh
where h is a hexadecimal digit.
If you only want to match a hexadecimal number, the literal text expression for the regexp is:
/xhh/ or /xhhhh/
Word Boundary
Consider the following strings:
“one two three four five”
“one,two,three,four,five”
“one, two, three, four, five”
“one-two-three-four-five”
The following expression will return true (produce a match) in a conditional:
/b/.test(“one two three four five”)
Actually it is the boundary between the opening double quotation mark and the word “one” that has been matched. The notation "b" is used to match a word boundary. If you want to match the boundary between the word “one” and the space that follows it, you have to modify the regexp to:
/oneb/
Here, you have the word "one" followed by "b."
The following expression will return true (produce a match) in a conditional:
/oneb/.test(“one two three four five”)
“b” indicates a word boundary. The following expression will return false (not produce a match) in a conditional:
/onbe /.test(“one two three four five”)
This is because the character “b” at its position does not correspond to a word boundary (it is inside the word "one").
Now, the following expression will return true (produce a match) in a conditional:
/twob/.test(“one,two,three,four,five”)
Here the string portion "two" is what has been matched. The “b” corresponds to the boundary between the word “two” and the comma that follows it. The following expression will also produce a match in a conditional:
/twob/.test(“one, two, three, four, five”)
Even though there is a space between the comma and the word “three,” the “b” still corresponds to the boundary between the word “two” and the comma that follows it.
Now, the following expression will return true (produce a match) in a conditional:
/threeb/.test(“one-two-three-four-five”)
Here the string portion "three" is what has been matched. The “b” corresponds to the boundary between the word “three” and the character “-” that follows it. The character “-” is a word separator; it separates two words joined together.
The following expression will return true (produce a match) in a conditional:
/fiveb/.test(“one two three four five”)
Here the “b” corresponds to the boundary between the word “five” and the closing double quotation mark.
Combining with Other Characters
You can combine the special characters above with other characters, as we have seen in the previous section. The following expression will return true (produce a match) in a conditional:
/is /.test(“The NUL character is ”)
In the regexp, you have the word "is" followed by a space, and then the NUL character, "." Matching occurs towards the end of the available string. We have combined the special character “NUL” with the word “is” and a space.