Match from a class of characters

If you want to match against a set of characters, you can place the set inside []. For example, [abc] would mean any character a, b, or c:

var pattern = /[abc]/;
console.log(pattern.test('a')); //true
console.log(pattern.test('d')); //false

You can specify that you want to match anything but the pattern by adding a ^ (caret sign) at the beginning of the pattern:

var pattern = /[^abc]/;
console.log(pattern.test('a')); //false
console.log(pattern.test('d')); //true

One critical variation of this pattern is a range of values. If we want to match against a sequential range of characters or numbers, we can use the following pattern:

var pattern = /[0-5]/;
console.log(pattern.test(3)); //true
console.log(pattern.test(12345)); //true
console.log(pattern.test(9)); //false
console.log(pattern.test(6789)); //false
console.log(/[0123456789]/.test("This is year 2015")); //true

Special characters such as $ and period (.) characters either represent matches to something other than themselves or operators that qualify the preceding term. In fact, we've already seen how [, ], -, and ^ characters are used to represent something other than their literal values.

How do we specify that we want to match a literal [ or $ or ^ or some other special character? Within a RegEx, the backslash character escapes whatever character follows it, making it a literal match term. So [ specifies a literal match to the [ character rather than the opening of a character class expression. A double backslash (\) matches a single backslash.

In the preceding examples, we saw the test() method that returns true or false based on the pattern matched. There are times when you want to access occurrences of a particular pattern. The exec() method comes in handy in such situations.

The exec() method takes a string as an argument and returns an array containing all matches. Consider the following example:

var strToMatch = 'A Toyota! Race fast, safe car! A Toyota!'; 
var regExAt = /Toy/;
var arrMatches = regExAt.exec(strToMatch); 
console.log(arrMatches);

The output of this snippet would be ['Toy']; if you want all the instances of the pattern Toy, you can use the g (global) flag as follows:

var strToMatch = 'A Toyota! Race fast, safe car! A Toyota!'; 
var regExAt = /Toy/g;
var arrMatches = regExAt.exec(strToMatch); 
console.log(arrMatches);

This will return all the occurrences of the word oyo from the original text. The String object contains the match() method that has similar functionality of the exec() method. The match() method is called on a String object and the RegEx is passed to it as a parameter. Consider the following example:

var strToMatch = 'A Toyota! Race fast, safe car! A Toyota!'; 
var regExAt = /Toy/;
var arrMatches = strToMatch.match(regExAt);
console.log(arrMatches);

In this example, we are calling the match() method on the String object. We pass the RegEx as a parameter to the match() method. The results are the same in both these cases.

The other String object method is replace(). It replaces all the occurrences of a substring with a different string:

var strToMatch = 'Blue is your favorite color ?'; 
var regExAt = /Blue/;
console.log(strToMatch.replace(regExAt, "Red"));
//Output- "Red is your favorite color ?"

It is possible to pass a function as a second parameter of the replace() method. The replace() function takes the matching text as a parameter and returns the text that is used as a replacement:

var strToMatch = 'Blue is your favorite color ?'; 
var regExAt = /Blue/;
console.log(strToMatch.replace(regExAt, function(matchingText){
  return 'Red';
}));
//Output- "Red is your favorite color ?"

The String object's split() method also takes a RegEx parameter and returns an array containing all the substrings generated after splitting the original string:

var sColor = 'sun,moon,stars';
var reComma = /,/;
console.log(sColor.split(reComma));
//Output - ["sun", "moon", "stars"]

We need to add a backslash before the comma because a comma is treated specially in RegEx and we need to escape it if we want to use it literally.

Using simple character classes, you can match multiple patterns. For example, if you want to match cat, bat, and fat, the following snippet shows you how to use simple character classes:

var strToMatch = 'wooden bat, smelly Cat,a fat cat';
var re = /[bcf]at/gi;
var arrMatches = strToMatch.match(re);
console.log(arrMatches);
//["bat", "Cat", "fat", "cat"]

As you can see, this variation opens up possibilities to write concise RegEx patterns. Take the following example:

var strToMatch = 'i1,i2,i3,i4,i5,i6,i7,i8,i9';
var re = /i[0-5]/gi;
var arrMatches = strToMatch.match(re);
console.log(arrMatches);
//["i1", "i2", "i3", "i4", "i5"]

In this example, we are matching the numeric part of the matching string with a range [0-5], hence we get a match from i0 to i5. You can also use the negation class ^ to filter the rest of the matches:

var strToMatch = 'i1,i2,i3,i4,i5,i6,i7,i8,i9';
var re = /i[^0-5]/gi;
var arrMatches = strToMatch.match(re);
console.log(arrMatches);
//["i6", "i7", "i8", "i9"]

Observe how we are negating only the range clause and not the entire expression.

Several character groups have shortcut notations. For example, the shortcut d means the same thing as [0-9]:

Notation

Meaning

d

Any digit character

w

An alphanumeric character (word character)

s

Any whitespace character (space, tab, newline, and similar)

D

A character that is not a digit

W

A non-alphanumeric character

S

A non-whitespace character

.

Any character except for newline

These shortcuts are valuable in writing concise RegEx. Consider this example:

var strToMatch = '123-456-7890';
var re = /[0-9][0-9][0-9]-[0-9][0-9][0-9]/;
var arrMatches = strToMatch.match(re);
console.log(arrMatches);
//["123-456"]

This expression definitely looks a bit strange. We can replace [0-9] with d and make this a bit more readable:

var strToMatch = '123-456-7890';
var re = /ddd-ddd/;
var arrMatches = strToMatch.match(re);
console.log(arrMatches);
//["123-456"]

However, you will soon see that there are even better ways to do something like this.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset