Sre*_*ova 2 javascript regex replace
我正在使用javascript函数,在给定的字符串中我需要替换现在//只有一个斜杠/我有:
result= mystring.replace("\/\/", "/");
Run Code Online (Sandbox Code Playgroud)
它没有工作,我仍然得到双斜杠字符串,所以这是正确的正则表达式,以指示替换函数的双斜杠?
我已经尝试过:
编辑:
我正在使用它来更正保存在字符串中的URL,例如,有时该URL可能类似于:mywebpage/someparameter//someotherparameter并且双斜杠会产生问题,因此我需要将其替换为单个斜杠,如:mywebpage/someparameter/someotherparameter
Pra*_*lan 10
使用带有全局修饰符的正则表达式/\/\//(或/\/{2}/)来替换所有匹配项.
result= mystring.replace(/\/\//g, "/");
Run Code Online (Sandbox Code Playgroud)
console.log(
'hi// hello//123//'.replace(/\/\//g, '/')
)Run Code Online (Sandbox Code Playgroud)
如果是用作替换的字符串则无需转义
console.log("asd//qwe".replace("//","/"));
Run Code Online (Sandbox Code Playgroud)
如果它是正则表达式,则需要对其进行转义
console.log("asd//qwe".replace(/\/\//,"/"));
Run Code Online (Sandbox Code Playgroud)
现在,如果有多个集合,则需要使用带有全局修饰符的正则表达式。
console.log("asd//qwe".replace(/\/\//g,"/"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3891 次 |
| 最近记录: |