Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-30-2002, 11:25 AM   PM User | #1
Darksbane
New Coder

 
Join Date: Jun 2002
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Darksbane is an unknown quantity at this point
Regular Expression questions

Here is what I am trying to do:

I have an array of sentences. and I would like the user to be able to enter in a word which is then inserted into a regular expression to find all sentences with that word in it.

Here is an example
SentenceArray= new Array (
"I have a dog",
"dogs are good",
"cats are bad.")


The user then enters in the word "dog"

the regular expression should then look like this I think:
regexp = /dog/
and then an if is set up like

for (...) {
if (regexp.test(SentenceArray[X]){
do something
}
}

I am not exactly sure what I am doing wrong, but I have figured out that it doesn't like "regexp.test(SentenceArray[X])" and only seems to take some kind of form element. I tried a hidden field instead and that got it to stop giving an error, but I still never get any results.

Any help or suggestionw would be appreciated.
Darksbane is offline   Reply With Quote
Old 07-30-2002, 12:07 PM   PM User | #2
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
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.
mordred is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:55 AM.


Advertisement
Log in to turn off these ads.