Java Reference
In-Depth Information
Let's take the following string defi ned in myString as an example:
var myString = “JavaScript, VBScript and Perl”;
How could you match both JavaScript and VBScript using the same regular expression? The only
thing they have in common is that they are whole words and they both end in Script. Well, an easy
way would be to use parentheses to group the patterns Java and VB. Then you can use the ? special
character to apply to each of these groups of characters to make the pattern match any word having
zero or one instances of the characters Java or VB, and ending in Script.
var myRegExp = /\b(VB)?(Java)?Script\b/gi;
Breaking this expression down, you can see the pattern it requires is as follows:
1.
A word boundary: \b
2.
Zero or one instance of VB: (VB)?
3.
Zero or one instance of Java: (Java)?
4.
The characters Script : Script
5.
A word boundary: \b
Putting these together, you get this:
var myString = “JavaScript, VBScript and Perl”;
var myRegExp = /\b(VB)?(Java)?Script\b/gi;
myString = myString.replace(myRegExp, “xxxx”);
alert(myString);
The output of this code is shown in Figure 9-12.
Figure 9-12
If you look back at the special repetition characters table, you'll see that they apply to the item preceding
them. This can be a character, or, where they have been grouped by means of parentheses, the previous
group of characters.
However, there is a potential problem with the regular expression you just defi ned. As well as matching
VBScript and JavaScript, it also matches VBJavaScript. This is clearly not exactly what you meant.
To get around this you need to make use of both grouping and the special character | , which is the
alternation character. It has an or-like meaning, similar to || in if statements, and will match the
characters on either side of itself.
Search WWH ::




Custom Search