admin管理员组文章数量:1392893
how to remove \ (backslash ) symbol from string using javascript
var str ='Visit\ Microsoft \';
var res = str.replace(/\//g, "-");
alert(res);
how to remove \ (backslash ) symbol from string using javascript
var str ='Visit\ Microsoft \';
var res = str.replace(/\//g, "-");
alert(res);
Share
Improve this question
edited Mar 3, 2014 at 11:50
Rajiv Pingale
1,01510 silver badges27 bronze badges
asked Feb 22, 2014 at 14:58
user3319802user3319802
591 silver badge7 bronze badges
1
-
1
As posted, your JavaScript string will not have any backslash characters in it (and it's a backslash, not a "slash"). If it did, you'd need to use the regex
/\\/g
to find them. – Pointy Commented Feb 22, 2014 at 15:00
5 Answers
Reset to default 2Use:
str.replace(/\\/g, "-");
So you need to use backslash (\
) instead of forward slash (/
).
it should be
var str = 'Visit\\ Microsoft \\';
var res = str.replace(/\\/g, "-");
alert(res);
you need to escape the \
with another \
as back slash is a escape character, also the regex should be /\\/g
Demo: Fiddle
Another approach.
str = str.split('\\').join('').trim()
Have you noticed the color change in your HTML Code?
var str ='Visit\ Microsoft \';
var res = str.replace(/\//g, "-");
alert(res);
Everything after Microsoft is red, that indicates everything after Microsoft is also a string.
\
Backslash works as a except character, So it except closing "'
" mark.
Please check your code, you may need to add backslash before Space, like below
var str ='Visit\ Microsoft\ ';
var res = str.replace(/\//g, "-");
alert(res);
This Code will work as you wanted.
var str ='Visit\ Microsoft \'; will not work, coz its invalid..
var str ='Visit\\ Microsoft \\'; -- is the correct js string
本文标签: how to remove(backslash ) symbol from string using javascriptStack Overflow
版权声明:本文标题:how to remove(backslash ) symbol from string using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744789990a2625229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论