0

I currently have the following:

var sText = 'I like stackoverflow';

if ( $(this).text().match( eval("/" + sText + "/ig") ) ) {

As you can see, this matches the entire string.

I need to match case insensitive text that contains all the words individually. So it will look for "I", "like" and "stackoverflow". It must have all 3 words.

Much appreciate the help.

6
  • 2
    So you want it to match both "I like stackoverflow" and in yoda form, "stackoverflow I like" but also string like "I dont like stackoverflow"? Question isn't clear Commented Oct 10, 2011 at 15:09
  • Yes and "i sTackOverflow Like"! Commented Oct 10, 2011 at 15:11
  • instead of eval, you could use RegExp object, which takes two arguments. First is a string (basically regexp, but without the trailing slashes), second is a string of flags ('ig' in your case). On the subject however, if I understood you correctly, var sText = "(i|like|stackoverflow)"; should do the trick? If it's correct, I'll add is at answer, if it isn't, be more specific. Commented Oct 10, 2011 at 15:12
  • Are you attached to RegEx? Javascript also has a String split method as well. Maybe I'm not understanding the question though. Commented Oct 10, 2011 at 15:12
  • Does it have to match whole words only? Commented Oct 10, 2011 at 15:13

4 Answers 4

1

If you really need to do this with a match, you can use multiple positive lookahead patterns. For example, to match a, b, and c, in any order:

> p = /(?=.*a)(?=.*b)(?=.*c)/i

> 'abc'.match(p)
[""]

> 'cba'.match(p)
[""]

> 'ac'.match(p)
null
Sign up to request clarification or add additional context in comments.

2 Comments

All i needed was the regex but thanks - you rule. Fixed with: eval("/(?=.*" + aText.join(')(?=.*') + ")/ig"))
I'd be interested to see this benchmarked for a fairly large value of sText especially compared with splitting it into individual regexes per word. I can't be bothered though! This would match "like Stackoverflow" btw since it lacks word boundaries, and like contains i
0

Why use regular expressions when .indexOf will do the job?

var str = $(this).text().toLowerCase();
if (str.indexOf('i')>=0 && str.indexOf('like')>=0 && str.indexOf('stackoverflow')>=0) {  
    // ...
}

(Mind you, in this case the "i" search is wasted since it's contained in "like", but I assume your actual code is searching for more ordinary text.)

Possible related question: JavaScript: case-insensitive search

3 Comments

The text may be 500 words long(+/-)
@Haraldo: All the more reason to avoid regular expressions. (Or was that comment meant for another answer?)
No I was looking at your conditions.
0

You could just split then loop -

var sText = 'I like stackoverflow';
var cText = 'stackoverflow like I';
var arr = sText.split(' ');
var match = true;

for (i=0;i<arr.length;i++) {
  if (!cText.match(eval("/" + arr[i] + "/ig"))) match= false;
}

alert(match);

You'd need to keep efficiency in mind if you were comparing lots of words though.

Comments

0

I think this is what you're looking for - the key is the word boundaries.

    var text = "text to match"

    var stringToLookFor = "I like stackoverflow";
    var words = stringToLookFor.split(" ");
    var match=true;
    $.each(words, function(index, word) {
        if(!text.match(eval("/\\b" + word + "\\b/i"))) {
            match=false;
            return false;
        }
    });

JSFiddle example.

3 Comments

I added return false which will terminate the loop if a word does not match, yielding improved performance.
Thanks for your time - I seemed to fix it with Piet Delport's solution.
@Haraldo does his method give a false positive match on "like stackoverflow"? Mine does not.

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.