Match with a Regular Expression, REGEX, in Javascript
You have a variable named jiji that contains a sentence.
var jiji="I see London.";
You name your regular expression variable huhu and write a pattern for what you are looking for. (We aren't writing any regex patterns here so the code is legible. We'll just use the word "london", but this is where you put your regex. - example: \s for spaces \n for unix line returns, etc.)
var huhu=/london/i;
You test the variable jiji for the regex variable huhu
if (huhu.test(jiji)) {
document.write("Matches!");
}
else {
document.write("Doesn't Match!");
}
NOTES:
-The i at the end of huhu makes the match pattern case-insensitive. Remove the i to make match case-sensitive.
-test() is a built-in Javascript method
Copy and Paste Javascript Code:
<script type=text/javascript>
<!--
var jiji="I see London.";
var huhu=/london/i;
if (huhu.test(jiji)) {
document.write("Matches!");
}
else {
document.write("Doesn't Match!");
}
-->
</script>
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
| | | |
| | | |
CSS David McFarland | | HTML Gary B. Shelly, Th... |