如何使用javascript替换正斜杠到反斜杠和正斜杠

how to replace the forward slash to backslash and forward slash using javascript

本文关键字:何使用 javascript 替换      更新时间:2023-09-26

我想使用javascript将"/"替换为"'/"。

例如:

http://www.freeformatter.com/javascript-escape.html

http:'/'/www.freeformatter.com'/javascript-escape.html

你可以这样做:

str.replace(/'//g, "''/");

其中str是保存字符串值的变量。

演示:http://jsbin.com/zijaqo/1/edit?js,控制台

use replace:

"http:/www.freeformatter.com/javascript-escape.html".replace(/'//g, "''/");

如果字符串在变量中:

var url = "http:/www.freeformatter.com/javascript-escape.html";
url.replace(/'//g, "''/");

这里可以使用str.replace()和正则表达式对象。基本上,您只需要转义替换字符串中的转义字符。

str = str.replace(new RegExp('/','g'),"''/");