use*_*094 6 javascript regex string replace node.js
我的节点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.";
Run Code Online (Sandbox Code Playgroud)
这里我想<b class =\"b_1\">*</b>用''. 输出是Your 1 payment due is $4000.Your 2 payment due is $3500. Your 3 payment due is $5000.。
如果这是一个正常的替换我不会有任何问题,但在这里我认为最好的替换方法是使用正则表达式。这是我感到困惑的地方。在java中我们有一个stringVariableName.replaceAll()方法。请让我知道我该怎么做。
谢谢
var newString = textToReplace.replace(/<b.*?>(.*?)<\/b>/g, '$1');
Run Code Online (Sandbox Code Playgroud)
说明:
<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
Run Code Online (Sandbox Code Playgroud)
然后我们用$1代表<b></b>标签内容的第一个捕获组替换整个匹配。
例子:
var newString = textToReplace.replace(/<b.*?>(.*?)<\/b>/g, '$1');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18972 次 |
| 最近记录: |