3

I created a regex with the string I am searching for by:

var re = new RegExp(searchTerm, "ig");

And i have an array that I want to search through that has these terms:

var websiteName = [
 "google", "youtube", "twitter", "medium", "amazon", "airbnb", "campaiyn", "uber", "dropbox", "asana",
 "slack", "soundcloud", "reddit", "uscitp", "facebook"
];

If my search term is reddit testtest test, I would not get a match when I call the match function:

 for(var i = 0; i < websiteName.length; i = i + 1) {
 if(websiteName[i].match(re) != null) {
 possibleNameSearchResults[i] = i;
 }
 }

How can i structure my regex statement so that when I search through my array that it will still return true if just one of the words match?

asked Mar 4, 2016 at 0:14
1
  • Why not iterate through the websiteName array and use each element as the regexp search term for the string. Commented Mar 4, 2016 at 0:20

1 Answer 1

3

I think you want something like this:

var searchTerm = 'reddit testtest test';
var websiteNames = ["google", "youtube", "twitter", "medium", "amazon", "airbnb", "campaiyn", "uber", "dropbox", "asana", "slack", "soundcloud", "reddit", "uscitp", "facebook"];
// filter the websiteNames array based on each website's name
var possibleNameSearchResults = websiteNames.filter(function(website) {
 // split the searchTerm into individual words, and
 // and test if any of the words match the current website's name
 return searchTerm.split(' ').some(function(term) {
 return website.match(new RegExp(term, 'ig')) !== null;
 });
});
document.writeln(JSON.stringify(possibleNameSearchResults))

Edit: If you want the index instead of the actual value of the item, you are probably better off going with a more standard forEach loop, like this:

var searchTerm = 'reddit testtest test',
 websiteNames = ["google", "youtube", "twitter", "medium", "amazon", "airbnb", "campaiyn", "uber", "dropbox", "asana", "slack", "soundcloud", "reddit", "uscitp", "facebook"],
 possibleNameSearchResults = []
// loop over each website name and test it against all of
// the keywords in the searchTerm
websiteNames.forEach(function(website, index) {
 var isMatch = searchTerm.split(' ').some(function(term) {
 return website.match(new RegExp(term, 'ig')) !== null;
 });
 
 if (isMatch) {
 possibleNameSearchResults.push(index);
 }
})
document.writeln(JSON.stringify(possibleNameSearchResults))

answered Mar 4, 2016 at 0:27
Sign up to request clarification or add additional context in comments.

2 Comments

thank you!. is there anywa to return the index of the search result instead of the actual value?
@ogk - Sure, I've edited my answer to show another example that provides the index instead of the value.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.