简体   繁体   English

如何在JavaScript中用点号替换字符串中所有出现的//(双斜线)

[英]how to replace all occurrences of // (double slash) in a string with dot sign in JavaScript

I need to replace all occurrences of // with .我需要将所有出现的 // 替换为 。 in a string, but the solution I have now replaces each slash with "."在一个字符串中,但我现在的解决方案将每个斜杠替换为“。”

So Folder//file//name becomes Folder..file..name所以 Folder//file//name 变成了Folder..file..name

It's a Node.js app and it didn't like replaceAll..这是一个 Node.js 应用程序,它不喜欢 replaceAll..

Code :代码

filename = filename.replace(/\//g, ".");

Group the experession:将表达式分组:

 const path = 'Folder//file//name'; console.log(path.replace(/(\\/\\/)/gm, "."));

尝试这个

filename.replace(/\/\//g, ".");
filename = filename.replace(/\//g, ".");

This code replaces all one / s with .此代码将所有的/ s 替换为. but if you want to replace double slashes // with one dot .但是如果你想用一个点替换双斜线// . , you should use: ,你应该使用:

filename = filename.replace(/\/\//g, ".");

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

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