Reputation: 11404
I'm trying to create a Regex with jQuery so it will search for two words in an attribute of an XML file.
Can someone tell me how to return a result that contains BOTH words (sport and favorite) in any order and any case (upper or lower case)?
var regex = new RegExp("sport favorite", 'i');
var $result = $(data).filter(function() {
if($(this).attr('Description')) {
return $(this).attr('Description').match(regex);
}
});
Upvotes: 1
Views: 100
Reputation: 413712
The "\b" marker can be used to "match" the edges of words, so
var regex = /\bsport\b.*\bfavorite\b|\bfavorite\b.*\bsport\b/i;
matches the words only as words, and (for example) won't match "sporting his favorite hat".
Upvotes: 0
Reputation: 9653
If they may be separated by any character, you could do it like this:
var regex = new RegExp(".*sport.+favorite.*|.*favorite.+sport.*", 'i');
(This assumes that no other word in the attribute contains the substring favorite or sport.)
Upvotes: 1
Reputation: 74360
var regex = new RegExp("sport favorite|favorite sport", 'i');
Upvotes: 1