PDA

View Full Version : Test-Bed For Regular Expressions


Philip M
05-08-2004, 06:44 PM
References:-

http://www.javascriptkit.com/javatutors/re.shtml

http://www.javascriptkit.com/javatutors/redev.shtml

The following is intended to provide a "test-bed" for trying out
regular expressions, or pattern matches. As JavaScript cannot
read from or write to a file the most usual application is likely to
be form validation.

The examples below by no means cover every aspect of regular
expression usage, but aim to provide examples of the most useful
constructs which the user can experiment with and adjust to suit his
own requirements. All use the .test() method. Once this is understood
other methods, particularly .replace(), should be easy to progress to.

The action to be taken if a match is found or not found will obviously
depend on the context and whether the matched (or unmatched)
characters are supposed to be prohibited, permitted or compulsory.

Switches:-

i means ignore case
g means global, that is test the whole string.
The \ character is escape and means "interpret the next character literally if it is a special character, or as a special character if it is a regular alphanumeric character". So \\ means literal backslash, \s means whitespace.


<HTML>
<HEAD>
</HEAD>

<BODY BGCOLOR="#FFF000" TEXT="#000000">

<br><tr>
<td><font face="Arial"><em><b>Enter some text here &nbsp &nbsp </b></em></font></td> <td><input type="text" size="25" maxlength="25" name="testext" onblur="isNumeric(this)"> </font></td></tr>

<SCRIPT = JavaScript>

function isNumeric(lstr) {
alert (lstr.value);

if (/one/gi.test(lstr.value)) { /// match the word one

// if (/^one/gi.test(lstr.value)) { /// match the word one at start of the string

// if (/\bone\b/gi.test (lstr.value)) { /// match one as a whole word in the string

// if (/o|n|e/gi.test(lstr.value)) { /// match o or n or e

// if (/[one]/gi.test(lstr.value)) { /// match o or n or e

// if (/(ab)|(jk)|(xy)/gi.test(lstr.value)) { /// match ab or kj or xy

// if (/(mon|tues|fri)day/gi.test(lstr.value)) { /// match the word Monday or Tuesday or Friday (case insenstive)

// if (/[^one]/gi.test(lstr.value)) { /// match anything except o or n or e

// if (/^[one]/gi.test(lstr.value)) { /// match o or n or e at start of string

// if (/[one]$/gi.test(lstr.value)) { /// match o or n or e at end of string

// if (/\d/.test(lstr.value)) { /// match any digit 0-9

// if (/\D/.test(lstr.value)) { /// match any non-digit

// if (/\D{2,4}\d{2,4}/.test(lstr.value)) { /// match 2-4 non-digits followed by 2-4 digits

// if (/^\d{5}$/.test(lstr.value)) { /// match 5 digits and nothing but

// if (/\w/.test(lstr.value)) { /// match any alphanumeric character

// if (/\W/.test(lstr.value)) { /// match any non-alphanumeric character

// if (/\s{2,}/.test(lstr.value)) { // match two or more whitespace characters

// if (/o.e/gi.test(lstr.value)) { /// match o followed by any character followed by e (one, OBE, o5x etc.)

// if (/[^0-9\s\-\'\"]/gi.test(lstr.value) || (lstr.value=="")) { /// only digits, space, hyphen or single/double quotes allowed

// if (/(\w+)\s+\1/gi.test(lstr.value)) { /// match repeated word (ignoring case)

alert ("Test returned true - match was found");
return false; /// or return true if appropriate
}

alert ("Test returned false - match was not found");
return true; /// or return false if appropriate
}

</SCRIPT>

</BODY>
</HTML>