0

I'm trying to search a string for any word of another string with javaScript. Let's say I have the following string that I want to search in:

"Oxford Street 100 London"

And my search term is like this:

"oxford london"

The above searchterm should give me a match since oxford and london is a part of the string to search in.

I have tried str.indexOf("oxford london") !== -1 but that doesn't work since it's not a combined word in the string to search in.

Is there any way of doing this?

3
  • Should it match because both the words are in it, or would 1 do - what if you searched for "oxford manchester"? Commented Apr 20, 2013 at 14:49
  • searching for "oxford manchester" should also give a match since the string contains "oxford" :-) Commented Apr 20, 2013 at 14:50
  • new RegExp("oxford london".split(/\W+/).join("|"), 'i') ? Commented Apr 20, 2013 at 14:53

4 Answers 4

1

You need to split the search terms by a space and search for each (case inensitively, I presume)

var terms = terms.split(' '),
    match = terms.every(function (term) {
        return str.toLowerCase.indexOf(term) > -1;
    });
Sign up to request clarification or add additional context in comments.

4 Comments

forEach? Do you mean filter?
Use .every instead of trying to filter with forEach (which doesn't even work)
Yes I meant filter, but every is even better
You should use some instead of every. Now its only true if all keys are found, while the OP said that at least one key must be found.
1

I would just use a simple regular expression:

if("London, Oxford street".match(new RegExp("oxford london".split(/\W+/).join("|"), 'i'))) {
    alert("found");   
}

5 Comments

That's not using the search string "oxford london" though.
also, does not match "London, Oxford street"
Yup you guys were right, updated it with the code stolen from user2264587.
Hello, tried it with if("London,Oford street"... and it also reported "found". Should be also somehow fixed.
Use regex.test(str) instead of str.match(regex) !== null
1
mystring = "Oxford Street 100 London"
search = "oxford london"
// split search string, by space, or by another delimiter if you like
terms = search.split(" ")
matches = false
// loop through all terms, assuming that matches is true if no negative comparisons are made
for(i=0; i< terms.length; i++){
  // make sure you lowercase both search string, and comparison string
  if(mystring.toLowerCase().indexOf(terms[i].toLowerCase())){
    matches = true
  }
}
// matches is true if any terms are found, and false if no term is found

8 Comments

@Archer Please do not add such significant modifications to a post without the original author's consent.
@RobW It's now wrong - no problem. I'll leave it the way you prefer :)
@RobW I expect it was by accident, I was adding stuff at the same time, so he probably did not know he was deleting my additions
@Archer, I could be wrong, but I don't think it is wrong. Matches starts off being assumed to be true - and if any of the terms are compared, and don't match, it turns to false. Seems like good logic to me.
It needs to match if either of the words are found - not both ;)
|
0

This works:

var str = "Oxford Street 100 London";
var terms = "oxford london";
terms = terms.replace(" ", "|");          // "oxford|london"
var find = new RegExp(terms, "g");        // /oxford|london/g
alert(str.toLowerCase().match(find));     // ["oxford", "london"]

see this fiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.