如何全局替换 JavaScript 字符串中的正斜杠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4566771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:00:11  来源:igfitidea点击:

How to globally replace a forward slash in a JavaScript string?

javascript

提问by Babiker

How to globally replace a forward slash in a JavaScript string?

如何全局替换 JavaScript 字符串中的正斜杠?

回答by Seldaek

The following would do but only will replace one occurence:

以下会做,但只会替换一次出现:

"string".replace('/', 'ForwardSlash');

For a global replacement, or if you prefer regular expressions, you just have to escape the slash:

对于全局替换,或者如果您更喜欢正则表达式,您只需要转义斜杠:

"string".replace(/\//g, 'ForwardSlash');

回答by BoltClock

Use a regex literal with the gmodifier, and escape the forward slash with a backslash so it doesn't clash with the delimiters.

使用带有g修饰符的正则表达式文字,并用反斜杠转义正斜杠,这样它就不会与分隔符发生冲突。

var str = 'some // slashes', replacement = '';
var replaced = str.replace(/\//g, replacement);

回答by PleaseStand

Without using regex (though I would only do this if the search string is user input):

不使用正则表达式(虽然我只会在搜索字符串是用户输入的情况下这样做):

var str = 'Hello/ world/ this has two slashes!';
alert(str.split('/').join(',')); // alerts 'Hello, world, this has two slashes!' 

回答by Hemlock

Is this what you want?

这是你想要的吗?

'string with / in it'.replace(/\//g, '\');

回答by Marty

This has worked for me in turning "//"into just "/".

这对我转变"//"为 just 有用"/"

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

回答by Baz

You need to wrap the forward slash to avoid cross browser issues or //commenting out.

您需要包装正斜杠以避免跨浏览器问题或//注释掉。

str = 'this/that and/if';

var newstr = str.replace(/[/]/g, 'ForwardSlash');

回答by Vinay

Hi a small correction in the above script.. above script skipping the first character when displaying the output.

嗨,上面脚本中的一个小更正……上面的脚本在显示输出时跳过了第一个字符。

function stripSlashes(x)
{
var y = "";
for(i = 0; i < x.length; i++)
{
    if(x.charAt(i) == "/")
    {
        y += "";
    }
    else
    {
        y+= x.charAt(i);
    }
}
return y;   
}

回答by Jeff Luyet

This is Christopher Lincolns idea but with correct code:

这是克里斯托弗林肯的想法,但代码正确:

function replace(str,find,replace){
    if (find != ""){
        str = str.toString();
        var aStr = str.split(find);
        for(var i = 0; i < aStr.length; i++) {
            if (i > 0){
                str = str + replace + aStr[i];
            }else{
                str = aStr[i];
            }
        }
    }
    return str;
}

Example Usage:

示例用法:

var somevariable = replace('//\\/\/sdfas/\/\/\\////','\/sdf','replacethis\');

Javascript global string replacement is unecessarily complicated. This function solves that problem. There is probably a small performance impact, but I'm sure its negligable.

Javascript 全局字符串替换是不必要的复杂。这个功能解决了这个问题。性能影响可能很小,但我相信它可以忽略不计。

Heres an alternativefunction, looks much cleaner, but is on average about 25 to 20 percent slower than the above function:

这是一个替代函数,看起来更简洁,但平均比上述函数慢 25% 到 20%:

function replace(str,find,replace){
    if (find !== ""){
        str = str.toString().split(find).join(replace);
    }
    return str;
}

回答by Christopher Lincoln

var str = '/questions'; // input: "/questions"
while(str.indexOf('/') != -1){
   str = str.replace('/', 'http://stackoverflow.com/');
}
alert(str); // output: "http://stackoverflow.com/questions"

The proposed regex /\//gdid not work for me; the rest of the line (//g, replacement);) was commented out.

提议的正则表达式/\//g对我不起作用;该行 ( //g, replacement);)的其余部分已被注释掉。