Previous Example-|-Next Example-|-Return to Chapter Listing

Example 14.17:
Regular Expressions - Finding Word Boundaries

[This example works with Netscape Navigator 4. It won't work with Netscape Navigator 2 or 3, or Internet Explorer 3.]

This time we're looking for a word boundary, the point at which a character meets a "non-word" character. Click the button and the script will search for the characters is, preceded and followed by any character other than a letter, number, or underscore. If the text is found, it's replaced with was.

Enter some text:

Results (the number of characters found):


Here's the function: <SCRIPT language="JavaScript"> <!-- function matchIt(form) { txt = form.data.value pat = /\bis\b/ txt2 = txt.replace(pat,"was") form.results.value = txt2 } // --> </SCRIPT> Here's the form: <FORM name="matchForm"> Enter some text: <INPUT type="text" name="data" size=80 value="This is a test. You can enter other text here."><P> <INPUT type="button" Value="Do the match" onclick="matchIt(this.form)"><P> Results (the number of characters found): <INPUT type="text" name="results" size=40><P> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing