06.07.2017 Views

Mastering JavaScript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

An example could be /^([XYZ])a\1/, which matches a string that starts with any<br />

of the X, Y, or Z characters followed by an a and followed by whatever character<br />

matched the first capture. This is very different from /[XYZ] a[XYZ]/. The character<br />

following a can't be any of X, or Y, or Z, but must be whichever one of those that<br />

triggered the match for the first character. Backreferences are used with String's<br />

replace() method using the special character sequences, $1, $2, and so on. Suppose<br />

that you want to change the 1234 5678 string to 5678 1234. The following code<br />

accomplishes this:<br />

var orig = "1234 5678";<br />

var re = /(\d{4}) (\d{4})/;<br />

var modifiedStr = orig.replace(re, "$2 $1");<br />

console.log(modifiedStr); //outputs "5678 1234"<br />

In this example, the regular expression has two groups each with four digits. In the<br />

second argument of the replace() method, $2 is equal to 5678 and $1 is equal to<br />

1234, corresponding to the order in which they appear in the expression.<br />

Greedy and lazy quantifiers<br />

All the quantifiers that we discussed so far are greedy. A greedy quantifier starts<br />

looking at the entire string for a match. If there are no matches, it removes the last<br />

character in the string and reattempts the match. If a match is not found again, the<br />

last character is again removed and the process is repeated until a match is found or<br />

the string is left with no characters.<br />

The \d+ pattern, for example, will match one or more digits. For example, if your<br />

string is 123, a greedy match would match 1, 12, and 123. Greedy pattern h.+l would<br />

match hell in a string hello—which is the longest possible string match. As \d+ is<br />

greedy, it will match as many digits as possible and hence the match would be 123.<br />

In contrast to greedy quantifiers, a lazy quantifier matches as few of the quantified<br />

tokens as possible. You can add a question mark (?) to the regular expression to<br />

make it lazy. A lazy pattern h.?l would match hel in the string hello—which is the<br />

shortest possible string.<br />

The \w*?X pattern will match zero or more words and then match an X. However, a<br />

question mark after * indicates that as few characters as possible should be matched.<br />

For an abcXXX string, the match can be abcX, abcXX, or abcXXX. Which one should<br />

be matched? As *? is lazy, as few characters as possible are matched and hence the<br />

match is abcX.<br />

With this necessary information, let's try to solve some common problems using<br />

regular expressions.<br />

[ 85 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!