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

Example 14.19:
Regular Expressions - Using split()

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

This script remembers what it finds. But it also uses the split() method to remove all the characters specified (the separator characters) and keep the rest. Type a phone number into the text box. You don't have to use the "3, 3, and 4 character" format this time. Type any groups of numbers separated by any non-digit characters, and the script will extract the numbers and display them in alert box.


Here's the function: <SCRIPT language="JavaScript"> <!-- function phoneNumber(form) { var s = form.data.value var num = new Array() var p = /\D+/ num = s.split(p) alert("The result is " + num) } // --> </SCRIPT> Here's the form: <FORM name="telnum"> <INPUT type="text" name="data" size=32> <INPUT type="button" value="Scan it" onclick="phoneNumber(this.form)"> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing