[英]how to replace the forward slash to backslash and forward slash using javascript
我想使用javascript將“ /”替換為“ \\ /”。
例如:
http://www.freeformatter.com/javascript-escape.html
至
http:\/\/www.freeformatter.com\/javascript-escape.html
使用替換:
"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'),"\\/");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.