3

I'm trying to search a string and find multiple matches at once from an array.

I joined my array items with '|', but I'm getting null.

var searchTerms = ['dog', 'cat'];
var str = 'I have a dog and a cat';

// want to return matches for both dog and cat
console.log(str.match('/' + searchTerms.join('|') + '/g'));

http://jsfiddle.net/duDP9/1/

5 Answers 5

5

Use RegExp like this:

var searchTerms = ['dog', 'cat'];
var str = 'I have a dog and a cat';

console.log(str.match(new RegExp(searchTerms.join('|'), 'g')));
Sign up to request clarification or add additional context in comments.

Comments

2

You could also use Array.every(), along with Str.indexOf, which returns -1 if the string isn't found:

var searchTerms = ['dog', 'cat'];
var str = 'I have a dog and a cat';
searchTerms.every(search => str.indexOf(search) !== -1);
// true
str = 'I only have a dog';
searchTerms.every(search => str.indexOf(search) !== -1);
// false

Array.every returns true if the callback returns true for every element of the array.

The advantage of using this approach over regular expressions is that there is less chance that characters in your searchTerms will be interpreted in unexpected ways.

Comments

1

If you want to dynamically create a RegExp object from a string you need to use the constructor:

console.log(str.match(new RegExp(searchTerms.join('|'), 'g')));

However, I'd generally recommend some other approach. If your search terms contain a special regex character (e.g. $), then this is likely to fail. Of course you could escape those special characters, but still, I'd recommend you look for some other solution if possible.

It's hard to say exactly what solution would look like, since I don't know the full use case, but here's a very simple example of an alternative solution:

var str = 'I have a dog and a cat';
var searchTerms = {'dog': 1, 'cat': 1};
console.log(str.split(/\s+/).filter(function(x) { return x in searchTerms; }));
// [ "dog", "cat" ]

6 Comments

could you make a recommendation on "some other approach" :D
@user3448187 See my updated answer for an alternative.
very, very interesting. If the string is very long, would you say regex is faster?
@user3448187 Well, it might be faster, it might be slower. That depends on a lot of factors. In any case if it's really long, I would probably try to do a lot of this processing on the server, possibly with something like a full-text search in SQL
thanks! It's not that long, I'm just a little paranoid on performance for some strange reason. :D I'll keep digging around with breaking the string up into words
|
1

You need to convert the string to regular expression using new RegExp(..., 'g')

console.log(str.match(new RegExp(searchTerms.join('|'), 'g')));

2 Comments

this only returns cat for me
@user3448187, '/' + should be removed. I updated the answer. Check it again.
0

The syntax of what you want to do is (option1|option2|...|optionN) watch out the parentheses, I don't think you've added them.

Try to use: console.log(str.match('/(' + searchTerms.join('|') + ')/g'));

Comments

Your Answer

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