2

I have a string with keywords, separated by comma's.

Now I also have a nice RegEx, to filter out all the keywords in that string, that matches a queried-string.

Check out this initial question - RegEx - Extract words that contains a substring, from a comma seperated string

The example below works fine; it has a masterString, and a resultString. That last one only contains the keywords that has at least the word "car" in it.

masterString = "typography,caret,car,align,shopping-cart,adjust,card";
resultString = masterString.match(/[^,]*car[^,]*/g);
console.log(resultString);

Result from the code above;

"caret", "car", "shopping-cart", "card"

But how can I use the RegEx, with a variable matching-word (the word "car" in this example static and not variable).

I think it has to do something with a RegExp - but I can't figure out...

5
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 2, 2014 at 14:02
  • Rather than define a new RexExp with a string where you have to replace |QUERY| with something, why not just do string concatenation? That's how the RegExp object was meant to be used. Commented Aug 2, 2014 at 15:28
  • Thx - I added another solution (#2) which does the job, as you said! Commented Aug 2, 2014 at 15:38
  • @ElBandito try typing something like he(]o in your search box :) Commented Aug 2, 2014 at 15:42
  • @LUCAS - yeah :) But I also cleaned up the query, after submitting and before validating... See the code I used for that overhere... i.imgur.com/zC0NzRd.jpg In my case it's safe to use this solution, because it's used in an app, with little user-control (I control all the codes and there behaviour). Commented Aug 2, 2014 at 16:34

3 Answers 3

2

Here's a general solution for use with regexes:

var query = "anything";    

// Escape the metacharacters that may be found in the query
// sadly, JS lacks a built-in regex escape function
query = query.replace(/[-\\()\[\]{}^$*+.?|]/g, '\\$&');

var regex = new RegExp("someRegexA" + query + "someRegexB", "g");

As long as someRegexA and someRegexB form a valid regex with a literal in-between, the regex variable will always hold a valid regex.

But, in your particular case, I'd simply do this:

var query = "car";
var items = masterString.split(",");
query = query.toLowerCase();
for (var i = 0; i < items.length; ++i) {
    if (items[i].toLowerCase().indexOf(query) >= 0) {
        console.log(items[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

How about this one?, you only need to replace \ \ with String , and it works for me. it can find whether your string has "car", not other similar word

 var query  = 'car';
 var string = "car,bike,carrot,plane,card";
 var strRegEx = '[^,]*'+query+'[,$]*'; 
 string.match(strRegEx);

1 Comment

I picked this one as the answer, because it comes closest to the one I figured out myself (see Update 2). But the other solution on this page is very good as well; They both do their job - thx!
0

Answer provided by OP and removed from inside the question

I figured out this quick-and-maybe-very-dirty solution...

var query  = 'car';
var string = "car,bike,carrot,plane,card";
var regex  = new RegExp("[^,]*|QUERY|[^,]*".replace('|QUERY|',query),'ig');
string.match(regex);

This code outputs the following, not sure if it is good crafted, 'though..

"car", "carrot", "card"

But ended figuring out another, much simpler solution;

var query  = "car";
var string = "car,bike,carrot,plane,card";
string.match(new RegExp("[^,]*"+query+"[^,]*",'ig'));

This code outputs the string below;

["car", "carrot", "card"]

My app-search-engine now works perfect :)

Comments

Your Answer

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.