Javascript 节点js中的字符串regex replace
我的节点js中有下面的字符串Javascript 节点js中的字符串regex replace,javascript,node.js,regex,string,replace,Javascript,Node.js,Regex,String,Replace,我的节点js中有下面的字符串 var textToReplace = "Your <b class =\"b_1\">1</b> payment due is $4000.Your <b class =\"b_1\">2</b> payment due is $3500. Your <b class =\"b_1\">3</b> payment due is $5000."; var textToReplace=“您的
var textToReplace = "Your <b class =\"b_1\">1</b> payment due is $4000.Your
<b class =\"b_1\">2</b> payment due is $3500. Your <b class =\"b_1\">3</b>
payment due is $5000.";
var textToReplace=“您的1笔到期付款为4000美元。您的
2到期付款为3500美元。您的3
应付款为5000美元。”;
这里我想用'
替换*
。输出为您的1笔到期付款为4000美元。您的2笔到期付款为3500美元。您的3笔到期付款为5000美元。
如果这是一个正常的替换,我不会有任何问题,但在这里,我认为最好的替换方法是使用正则表达式。这就是我困惑的地方。在java中,我们有一个stringVariableName.replaceAll()
方法。请让我知道我该怎么做
谢谢,您不一定需要正则表达式,但是使用jQuery可以轻松删除子项并使用经典的一行程序返回HTML:
textReplaced = $('<div>').html(textToReplace).children().remove().end().html();
textReplaced=$('').html(textToReplace.children().remove().end().html();
这里有一把小提琴-我会用这样的东西:
textToReplace.replace(/<b class =\"b_1\">[0-9]+<\/b>/g, '');
textToReplace.replace(/[0-9]+//g');
您只需将与以下/(.*)/g
一起使用,就可以像这样在代码中使用它:
var textToReplace=“您的1笔到期付款为4000美元。您的2笔到期付款为3500美元。您的3笔到期付款为5000美元。”;
console.log(textToReplace.replace(/(.*)/g,$1')代码>var newString=textToReplace.replace(/\s*/g',)代码>。Regex101。阅读有关全局修饰符和非贪婪正则表达式的内容。@ibrahimmahrir,感谢您的快速回复。我的错,我实际上忘记了实际替换的字符串,请您查看我更新的问题。var newString=textToReplace.replace(/(.*)/g,$1')代码>。Regex101.ehem-ehem。他点了点头。
var newString = textToReplace.replace(/<b.*?>(.*?)<\/b>/g, '$1');
<b.*?> : matches the <b ...> opening tag (using the non-greedy quantifier to match as few as possible)
(.*?) : matches the content of the <b></b> tag (should be grouped so it will be used as a replacement text), it uses the non-greedy quantifier too.
<\/b> : matches the closing tag
g : global modifier to match as many as possible