0

I have html content where some words have success, failure etc. I am trying to use a regular expression to filter with single keyword or combination of keywords from multi selection checkboxes. The result of multi selection checkboxes is a comma separated string. Is there any condition to check if my html content has success or failure or other string cases. I am trying to search for or clause with regular expression

Case 1

var filter = 'success';
$(this).html().search(new RegExp(filter, "i")) > 0) {
alert('data found')
}

Case 2:

var filter = 'success, failure';
$(this).html().search(new RegExp(filter, "i")) > 0) {
alert('data found')
}
rockerest
  • 10,412
  • 3
  • 37
  • 67
Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • 1
    Your filter is not a (meaningful) regular expression, except to match the exact string you've typed. Check out http://regexone.com/ or http://www.regexpal.com/ to learn about regular expressions. – rockerest Apr 03 '16 at 08:04
  • 1
    First of all: when searching whole words, use the regex `\bWORD\b` (where WORD is the word you’re searching for). Second: use `.text()` instead of `.html()` to search in the text content of an HTML node; otherwise you’ll find the word also in tag names, attributes, etc. Third: I don’t know your exact use case but it’s probably easier to use a regex literal instead of calling `new RegExp` with a string argument. – Raphael Schweikert Apr 03 '16 at 08:05
  • 1
    Also your condition should be `> -1` or `>= 0` instead of `> 0` because the match might occur at index `0`. – Raphael Schweikert Apr 03 '16 at 08:29
  • 1
    Is there a specific reason you need to use a regular expression? You can just use an `if` with or `||` : `if (text.indexOf("success")>=0 || text.indexOf("fail")>=0)` – freedomn-m Apr 03 '16 at 08:34

3 Answers3

2

Define one array containig your expected string

var arrString = ["success","failure"];

As you say,multi selection checkboxes is a comma separated string.

So, I am assuming like this.

var arrHtml = "success,test,test2,failure";  ////$(this).html()
arrHtml.split(",").some(function(val, index) {
    return arrString.indexOf(val) >= 0
})

OR ECMA6 :

arrHtml.split(",").some((val,index)=> arrString.indexOf(val)>=0 )
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
2

Let me just say that using regexes for this may be a bit of a solution looking for a problem and you might get much further using plain .indexOf() or the like as in @RIYAJ KHAN’s answer

If you have an array with all the words you’re looking for, for example:

var WORDS = ['success', 'failure'];

you can create your RegExp object as follows:

var expression = new RegExp('\\b('+WORDS.join('|')+')\\b');

(Using \b at beginning and end will ensure you only match whole words, which may be the only valid reason to use a regex for this.)

Note that JavaScript does not have a function to escape regex special characters from strings but if you don’t know whether your strings are regex-safe or not, use this to escape the array items first.

Then search for any word:

var found = expression.test($(this).text());

Note that if you want to find out which words matched, you’d have to use the String.prototype.match method instead of String.prototype.search.

Raphael Schweikert
  • 18,244
  • 6
  • 55
  • 75
1

try

var filter = '(success)|(failure)';
$(this).html().search(new RegExp(filter, "i")) > 0) {
alert('data found')
}
shrw
  • 1,719
  • 5
  • 27
  • 50