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 you are looking for.
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>