2016-02-15 119 views
1

我该如何更换〜〜的所有文本|与正则表达式在JavaScript?用正则表达式替换文本

test.html~1455551818474|test.html 

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

\|.*$ 

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

+4

不要忘记显示你想要的代码 – anubhava

+1

'str = str.replace(/〜。* \ |/g,'')' – anubhava

+1

@anubhava,请张贴它作为答案。 – ndn

回答

2

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

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

万一你想在输出管道字符,然后使用:

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

也记住.*是贪婪的,如果有多个这样的情况,将会在输入中找到~|之间的最长匹配。

0

什么:

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

+0

我需要删除中间的所有数字 – mruss24

+1

为什么不加入这个问题?不要忘记指定你需要的输出。试试'var resp = str.replace(/〜\ d */g,“”);'如果你需要'test.html | test.html' –