最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript create regex programmatically - Stack Overflow

programmeradmin2浏览0评论

I know that I can create a javascript replace like this:

str = str.replace(/mytarget/g, 'some value');

that will replace all the occurrences of the literal mytarget. However, I have a big array of words/phrases that I want to use in regex replace, and as regexps are just language elements (they are not wrapped in a string when declaring), I can't find a way to declare regexps programmatically unless I hard-code them. So if I have:

var arr=['word', 'another', 'hello'];

I want to produce:

str = str.replace(/word/g, 'some value');

str = str.replace(/another/g, 'some value');

str = str.replace(/hello/g, 'some value');

Please post an example that I can use regexps, as I'll be adding more expressions into the regexps such as whitespace etc. so I NEED it the regexp way. Finally, please don't offer using eval, I'm sure there is a better way.

I know that I can create a javascript replace like this:

str = str.replace(/mytarget/g, 'some value');

that will replace all the occurrences of the literal mytarget. However, I have a big array of words/phrases that I want to use in regex replace, and as regexps are just language elements (they are not wrapped in a string when declaring), I can't find a way to declare regexps programmatically unless I hard-code them. So if I have:

var arr=['word', 'another', 'hello'];

I want to produce:

str = str.replace(/word/g, 'some value');

str = str.replace(/another/g, 'some value');

str = str.replace(/hello/g, 'some value');

Please post an example that I can use regexps, as I'll be adding more expressions into the regexps such as whitespace etc. so I NEED it the regexp way. Finally, please don't offer using eval, I'm sure there is a better way.

Share Improve this question edited Aug 3, 2017 at 9:03 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked Oct 10, 2011 at 18:51 Can PoyrazoğluCan Poyrazoğlu 34.8k54 gold badges206 silver badges423 bronze badges 2
  • Why are you using regular expressions? Just do a simple string replacement. – Quentin Commented Oct 10, 2011 at 18:52
  • and the replace pattern simply is a regexp with the reasons I've explained above. – Can Poyrazoğlu Commented Oct 10, 2011 at 21:22
Add a comment  | 

4 Answers 4

Reset to default 27

You need to invoke the RegExp constructor function for that. Example:

['word', 'another', 'hello'].forEach(function( word ) {
    var myExp = new RegExp(word, 'g');
    str = str.replace(myExp, 'some value');
});

The first argument for the constructor is a string, which literally takes anything you would wrap inbetween //. The second paramter is also string, where you can pass modifiers like g, i, etc.

You can build a RegEx dynamically like this -

var word = 'hello';
var re =  new RegExp("\\b" + word + "\\b","g");

Remember that you'll need to escape any \ characters using \\

for( i in arr )
{
    str = str.replace(new RegExp("\\"+arr[i],"g"), "some value")
}

If you are generating the reg-exp on the fly, you can use the RegExp object

var a = new RegExp("word")
发布评论

评论列表(0)

  1. 暂无评论