Ajax software
Free javascripts
↑
Main Page
Matching a Single Character in JavaScript
You want to find all occurrences of uppercase
L
. You can express the simple task that you want to use regular
expressions to do as follows:
Match any occurrence of uppercase
L
.
You can see, using JavaScript as a sample technology, how most regular expression engines will match the
pattern
L
using the XHTML file
UpperL.html
, shown here:
<html>
<head>
<title>Check for Upper Case L</title>
<script language=”javascript” type=”text/javascript”>
var myRegExp = /L/;
function Validate(entry){
return myRegExp.test(entry);
} // end function Validate()
function ShowPrompt(){
var entry = prompt(“This script tests for matches for the regular expression
pattern: “ + myRegExp + “.\nType in a string and click on the OK button.”, “Type
your text here.”);
if (Validate(entry)){
alert(“There is a match!\nThe regular expression pattern is: “ + myRegExp + “.\n
The string that you entered was: ‘“ + entry + “‘.”);
} // end if
else{
alert(“There is no match in the string you entered.\n” + “The regular expression
pattern is “ + myRegExp + “\n” + “You entered the string: ‘“ + entry + “‘.” );
} // end else
} // end function ShowPrompt()
</script>
</head>
<body>
<form name=”myForm”>
<br />
<button type=”Button” onclick=”ShowPrompt()“>Click here to enter text.</button>
</form>
</body>
</html>
1.
Navigate in Windows Explorer to the directory that contains the file
UpperL.html
, and double-
click the file. It should open in your default browser.
2.
Click the button labeled Click Here To Enter Text. A prompt window is shown, as you can see in
Figure A-2.
314
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 314
Ajax software
Free javascripts
→