RegEx - JavaScript .match(),带有一个变量关键字 [英] RegEx - JavaScript .match() with a variable keyword

查看:817
本文介绍了RegEx - JavaScript .match(),带有一个变量关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含关键字的字符串,用逗号分隔。



现在我还有一个很好的 RegEx



查看这个初始问题 - RegEx - 从逗号分隔字符串中提取包含子字符串的单词



以下示例工作正常;它有一个masterString和一个resultString。最后一个仅包含至少包含汽车字样的关键字。

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

上述代码的结果;

 caret,car,shopping-cart,card

但是如何使用RegEx和一个可变的匹配字(在这个例子中, car 是静态的而不是可变的)。



我认为它必须用RegExp做一些事情 - 但我不能弄清楚...






更新1



我想出了这个快速,也许很脏的解决方案...

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

此代码输出以下内容,不知道它是否精心制作,'虽然..

 car,carrot,card




UPDATE 2



,更简单的解决方案;

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

此代码输出以下字符串:

  [car,carrot,card] 



< hr>

我的应用搜索引擎现在可以正常工作:) - 请参阅 http://i.imgur.com/RXwkrrF.png?1

解决方案

这个怎么样,你只需要替换\ \与String,它适用于我。它可以找到你的字符串是否有car,而不是其他类似的词

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


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...


UPDATE 1

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"


UPDATE 2

I figured 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 :) - see http://i.imgur.com/RXwkrrF.png?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);

这篇关于RegEx - JavaScript .match(),带有一个变量关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆