JavaScript Tutorial/Regular Expressions/Match

Материал из Web эксперт
Версия от 18:52, 25 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Match: [aA]

<html>
<head>
<title>Matching a single literal character</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingChar(str){
    var myRegExp = /[aA]/ ;
    return myRegExp.test(str)
}
function TestGuess(){
    alert(IsMatchingChar("A"));
    alert(IsMatchingChar("a"));
}
// -->
</script>
</head>
<body>
<form>
<button type="Button" onclick="TestGuess()">Click here to guess</button>
</form>
</body>
</html>


Match: AAA|BBB

<html>
<head>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingPerson(str){
    var myRegExp = /AAA|BBB/ ;
    return myRegExp.test(str)
}
function TestGuess(){
    alert(IsMatchingPerson("AAA"));
    alert(IsMatchingPerson("BBB"));
}
// -->
</script>
</head>
<body>
<form>
<button type="Button" onclick="TestGuess()">Click here to guess</button>
</form>
</body>
</html>


Match: [A-Z][0-9]

<html>
<head>
<title>Matching a sequence of literal characters</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingCode(str){
    var myRegExp = /[A-Z][0-9]/ ;
    return myRegExp.test(str)
}
function TestGuess(){
    alert(IsMatchingCode("a3"));
}
// -->
</script>
</head>
<body>
<form>
<button type="Button" onclick="TestGuess()">Click here to guess</button>
</form>
</body>
</html>


Matching a single literal character(It can be either a lower or upper case character)

<html>
<head>
<title>Matching a single literal character(It can be either a lower or upper case character) </title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingChar(str){
    var myRegExp = /[aA]/ ;
    return myRegExp.test(str)
}
function TestGuess(){
    alert(IsMatchingChar("A"));
}
// -->
</script>
</head>
<body>
<h3>This page allows you to guess a single
 literal character of either case</h3>
<form>
<button type="Button" onclick="TestGuess()">Click here to guess</button>
</form>
</body>
</html>


Matching sequences of more than one literal character (The format is two upper case letters, a dash and three numbers)

<html>
<head>
<title>Matching sequences of more than one literal character (The format is two upper case letters, a dash and three numbers)</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingCode(str){
    var myRegExp = /[A-Z]{2}-[0-9]{3}/ ;
    return myRegExp.test(str)
}
function TestGuess(){
    alert(IsMatchingCode("asdf"));
}
// -->
</script>
</head>
<body>
<h3>This page allows you to choose a six character parts code</h3>
<form>
<button type="Button" onclick="TestGuess()">Click here to guess</button>
</form>
</body>
</html>


Regular Expressions: /a/

<html>
<head>
<title>Matching a single literal character</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingChar(str){
    var myRegExp = /a/;
    return myRegExp.test(str)
}
function TestGuess(){
    var guess = prompt("Enter a lower case character here","a");
    if (IsMatchingChar(guess)){
        alert("The correct answer was " + guess);
    }
    else{
        alert("Sorry " + guess + " doesn"t match");
    }
}
// -->
</script>
</head>
<body>
<form>
<button type="Button" onclick="TestGuess()">Click here to guess</button>
</form>
</body>
</html>


Regular Expression Tester Form

<html>
<head>
<title>Regular Expression Tester</title>
<script type="text/javascript" language="javascript">
<!-- //
function TestString(){
    testString = document.SimpleForm.TestInput.value;
    var pattern = document.SimpleForm.FirstInput.value;
    myRegExp = new RegExp(pattern);

    if (myRegExp.test(testString)){
        alert("The test string: " +testString + "\nmatched the current pattern: " + myRegExp);
    } else{
        alert("The test string: " +testString + "\ndid not match the current pattern: " + myRegExp);
    }
}
function ClearAndSet(){
    document.SimpleForm.reset();
    SetFocus();
    MaxNum = 0;
}
// -->
</script>
</head>
<body>
<h3>This page allows you to create and test regular expression patterns</h3>
<form name="SimpleForm">
<table>
<tr>
 <td width="30%" align="right">Enter a pattern:</td>
 <td><input name="FirstInput" type="text"></td>
</tr>
<tr>
 <td width="30%" align="right">Enter a string to match the pattern:</td>
 <td><input name="TestInput" type="text"></td>
</tr>
<tr>
 <td width="25%" align="right"><button type="Button" onclick="ClearAndSet()">
Clear and Start again</button></td>
 <td><button type="Button" onclick="TestString()">
Click to test the string you entered</button></td>
</tr>
</table>
</form>
</body>
</html>


To match "b{0,1}rea{0,1}d"

<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var sToMatch = "bread, bed bead sead sea";
var reBreadReadOrRed = /b{0,1}rea{0,1}d/;
alert(reBreadReadOrRed.test(sToMatch)); 
    //-->
</SCRIPT>
</html>


To match words bread, read, or red

<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var sToMatch = "bread, bed bead sead sea";
var reBreadReadOrRed = /b?rea?d/;
alert(reBreadReadOrRed.test(sToMatch)); 
    //-->
</SCRIPT>
</html>