If you want to construct a Regular Expression from user input, then you'll have to use the new RegExp() constructor function to ensure runtime compilation of the newly created RegExp. See also the detailed information at
http://devedge.netscape.com/docs/man...p.html#1008311
A quick example for your code could look like
Code:
<html>
<head>
<title>RegExp</title>
<script>
SentenceArray= new Array (
"I have a dog",
"dogs are good",
"cats are bad.")
function matchIt(word) {
var regex = new RegExp(word, "i");
for (var i = 0; i < SentenceArray.length; i++) {
if (regex.test(SentenceArray[i])) {
alert("'" + word + "' was found in '" + SentenceArray[i] + "'");
}
}
}
</script>
</head>
<body>
<form onsubmit="matchIt(this.what.value); return false;">
what to match?
<input type="text" name="what"></input><br>
<input type="submit"></input>
</form>
</body>
</html>
I hope that helps you for adjusting your code to the desired functionality. Note that I used the "i" flag to make a case-
insensitive pattern matching.