用Regex替换文本

时间:2016-02-15 16:24:37

标签: javascript regex

如何将所有文本替换为〜用javascript中的正则表达式?

test.html~1455551818474|test.html

到目前为止,我知道如何删除|

\|.*$

我需要找出如何删除〜到|

2 个答案:

答案 0 :(得分:2)

您可以使用此正则表达式进行替换:

str = 'test.html~1455551818474|test.html';
str = str.replace(/~.*\|/g, '');
//=> test.htmltest.html

如果您想在输出中输入管道字符,请使用:

str = str.replace(/~.*\|/g, '|');
//=> test.html|test.html

另请注意.*是贪婪的,如果有多个匹配项,我会在输入~|之间找到最长匹配。

答案 1 :(得分:0)

怎么样:

var str = "test.html~1455551818474|test.html";
var resp = str.replace(/~/g, "|");
console.log(resp);