HTML and CSS Reference
In-Depth Information
EXAMPLE 17.30
<html>
<head><title>The Word Boundary</title></head>
<body>
<script type="text/javascript">
// Anchoring a word with \b
1
var reg_expression = /\blove\b/;
var textString=prompt("Type a string of text","");
2
var result=reg_expression.test(textString) ;// Returns true
// or false
document.write(result+"<br />");
if (result){
document.write("<b>The regular expression /\blove\b/
matched the string \""+ textString +"\".<br />");
}
else{
alert("No Match!");
}
</script>
</body>
</html>
EXPLANATION
1
The regular expression contains the \b metacharacter, representing a word bound-
ary, not a specific character. The expression reads: Find a word beginning and
ending with love . This means that gloves, lover, clover, and so on, will not be found.
2
The regular expression test() method will return true because the string love is
within word boundary anchors \b . See Figure 17.32.
Figure 17.32 The user entered Iloveyou! . The word love is between word
boundaries ( \b ). The match was successful.
17.4.6 Alternation
Alternation allows the regular expression to contain alternative patterns to be matched;
for example, the regular expression / John|Karen|Steve / will match a line containing John
or Karen or Steve . If Karen, John , or Steve are all on different lines, all lines are matched.
Each of the alternative expressions is separated by a vertical bar (the pipe symbol, | ) and
the expressions can consist of any number of characters, unlike the character class that
only matches for one character; thus, /a|b|c/ is the same as [abc] , whereas /ab|de/ cannot
 
 
Search WWH ::




Custom Search