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.

HGPB
  • 4,346
  • 8
  • 50
  • 86
  • 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 – Paul Creasey Oct 10 '11 at 15:09
  • Yes and "i sTackOverflow Like"! – HGPB Oct 10 '11 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. – zatatatata Oct 10 '11 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. – Mike Christensen Oct 10 '11 at 15:12
  • Does it have to match whole words only? – Chris Marasti-Georg Oct 10 '11 at 15:13
  • Zanfa - I've tried that before. I split the sText then join it with "|" e.g. (i|like|stackoverflow) however it still returns true if just stackoverflow is in the string. But I'll test a but more. – HGPB Oct 10 '11 at 15:24

4 Answers4

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
Pi Delport
  • 10,356
  • 3
  • 36
  • 50
  • All i needed was the regex but thanks - you rule. Fixed with: eval("/(?=.*" + aText.join(')(?=.*') + ")/ig")) – HGPB Oct 10 '11 at 15:45
  • 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` – Paul Creasey Oct 10 '11 at 16:31
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

Community
  • 1
  • 1
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
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.

ipr101
  • 24,096
  • 8
  • 59
  • 61
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.

Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137