...

View Full Version : Does this field contain any of these words?



snowball
06-15-2003, 01:03 PM
Hi

Can anyone help please .... It's really simple I think. At the moment I have a if statement as follows:

if (head == omit[0]) {

blah blah

}


It looks to see if head is equal to the first element of the omit array and if it is does something .....

But what I really want it to do is ....... if head (which is an ordinary sentence) contains any of the single words contained in the array called omit, then carry out the blah blah bit. Omit is an array of single words.

It should compare head to each of the elements of omit, and do blah blah, if any of the words in omit appear in head.

Can anyone suggest some code?

Many thanks

snow

Philip M
06-15-2003, 05:44 PM
Suggest you have a look at

http://javascript.internet.com/forms/smut-engine.html

which seems to do what you require.

beetle
06-15-2003, 07:03 PM
This will work

var searchWords = ['just','some','words'];
var sentence = "This is just a sentence";

String.prototoype.hasAnyWord = function( words )
{
var regex = new RegExp( "(" + words.join("|") + ")" );
return Boolean( regex.test( this ) );
}

if ( sentence.hasAnyWord( searchWords ) )
{
// blah blah
}

snowball
06-15-2003, 07:46 PM
Thanks guys .... but the smutengine doesn't quite do what's needed.

The second code doesn't seem to run on my page and produces an error:

'return' statement outside of function

Any ideas as to why? (It is exactly what's required if only it would work!)

Thanks

snow.

Owl
06-15-2003, 07:55 PM
Hi snow,

for(i=0; i<omit.length; i++)
if(head.indexOf(omit[i]) != -1) { alert(omit[i] + ' found in head') }

( ) ( )
>>V

beetle
06-15-2003, 08:46 PM
I modified my post to fix the error - sorry

snowball
06-16-2003, 01:46 PM
Hi Thanks for this .... It's working great with the variable list of words to exclude, but how do I get it to work with the array I have read in via:

<script>
var externalscript="http://www.outuk.com/style/omit.js"
</script>


The omit.js file looks like this:

omit = new Array('red', 'blue', 'green');


How do I switch the:

var searchWords = ['just','some','words'];
to be instead the red, blue and green (plus any other elements which might be added)

Thanks for all your help!

snow

beetle
06-16-2003, 02:16 PM
They are both arrays, I just defined one using the literal syntax. I could have just as easily used

var searchWords = new Array('just','some','words');

and probably should have, to avoid confusing you -- sorry.

:D

snowball
06-16-2003, 02:33 PM
Hi beetle ....

Thanks again for all the help .... I am really new to this, and almost getting it, but often get a little stuck.

I don't really want to define the contents of the searchWords array from within the code on that page but read it in from an outside file ..... the omit.js file. This is mainly because it will need to change quite often and I wouldn't want to have to re-write the page each time.

So how do I change:

var searchWords = new Array('just','some','words');

so that instead of being made up of a new array it uses the existing array contained in omit.js which looks like this:

omit = new Array('red', 'blue', 'green');


Really do appreciate your help.

snow

beetle
06-16-2003, 04:28 PM
You don't have to change anything except to send the appropriate array to the hasAnyWord() method

if ( head.hasAnyWord( omit ) )
{
// blah
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum