简体   繁体   English

替换 Javascript 中所有出现的字符串

[英]replace all ocurrence of a string in Javascript

I upload a picture to my Node API, and in my database the path of the picture is like this: public\\uploads\\img\\myImage-1551119543339.png , but I want to return to anyone requesting the imgs like this public/uploads/img/myImage-1551119543339.png .我将图片上传到我的 Node API,在我的数据库中图片的路径是这样的: public\\uploads\\img\\myImage-1551119543339.png ,但我想返回给任何请求像这样的 imgs public/uploads/img/myImage-1551119543339.png How do a save the path?如何保存路径?

All the back-slashes have to be escaped in the original string.所有反斜杠都必须在原始字符串中转义。 Then use split and join on the forward and back slash然后在正斜杠和反斜杠上使用 split 和 join

 var str="public\\\\uploads\\\\img\\\\myImage-1551119543339.png"; console.log(str.split("\\\\").join('/'))

You can use pattern to replace back slashes.您可以使用模式来替换反斜杠。

 var str="public\\\\uploads\\\\img\\\\myImage-1551119543339.png"; console.log(str.replace(/\\\\/g,'/'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM