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

时间:2022-05-31 21:19:08

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

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

8 个解决方案

#1


191  

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');

#2


22  

Use a regex literal with the g modifier, and escape the forward slash with a backslash so it doesn't * with the delimiters.

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

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

#3


8  

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!' 

#4


3  

Is this what you want?

这是你想要的吗?

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

#5


3  

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

这对我来说很有用,可以将“//”变成“/”。

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

#6


2  

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');

#7


1  

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;   
}

#8


0  

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

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

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

#1


191  

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');

#2


22  

Use a regex literal with the g modifier, and escape the forward slash with a backslash so it doesn't * with the delimiters.

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

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

#3


8  

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!' 

#4


3  

Is this what you want?

这是你想要的吗?

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

#5


3  

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

这对我来说很有用,可以将“//”变成“/”。

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

#6


2  

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');

#7


1  

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;   
}

#8


0  

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

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

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