javascript查找并替换字符串中的动态模式 [英] javascript find and replace a dynamic pattern in a string
本文介绍了javascript查找并替换字符串中的动态模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个动态模式,我一直在使用下面的代码来查找
I have a dynamic pattern that I have been using the code below to find
var matcher = new RegExp("%" + dynamicnumber + ":", "g");
var found = matcher.test(textinput);
我需要这个模式有一个新的要求,即包含额外的5个字符的尾随是或否。然后删除它或用''(没有)替换它。
I need the pattern to have a new requirement, which is to include an additional trailing 5 characters of either y or n. And then delete it or replace it with a '' (nothing).
我尝试了这种模式的语法,但显然它不起作用。
I tried this syntax for the pattern, but obviously it does not work.
var matcher = new RegExp("%" + dynamicnumber + ":" + /([yn]{5})/, "g");
赞赏任何提示
TIA。
推荐答案
您应该只将正则表达式字符串传递给RegExp c'tor:
You should only pass the regex string into the RegExp c'tor :
var re = new RegExp("%" + number + ":" + "([yn]{5})", "g");
这篇关于javascript查找并替换字符串中的动态模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文