Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Does this field contain any of these words?

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Suggest you have a look at

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

    which seems to do what you require.

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This will work
    Code:
    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
    }
    Last edited by beetle; 06-15-2003 at 08:45 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Owl
    Owl is offline
    New Coder
    Join Date
    Jun 2002
    Posts
    62
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi snow,

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

    ( •) (• )
    >>V

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I modified my post to fix the error - sorry
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #7
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #9
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't have to change anything except to send the appropriate array to the hasAnyWord() method

    if ( head.hasAnyWord( omit ) )
    {
    // blah
    }
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •