Simple regex question to remove \
开发者_运维百科I have a simple question how do I remove a \
with regex?
Thanks
I have tried it like this .replace(/\\/ig, '');
I am using javascript and classic asp
Now it works: .replace(/\\/ig, '');
somehow the first time I tried it, it were not working! - Thanks for all the quick answers!
I tested out the following and it worked (in a test bench) on the first \
(due to "\\"
), but not the second \
(because it was in a string, thus acting as an escape character).
<script type="text/javascript">
var str="\\ \ ";
document.write(str.replace(/\\/ig,"replaced"));
</script>
Are you sure that the \
you wish to replace is not just acting as an escape character and doesn't really exist in the output?
Double prefix with \ so become \\
Why regular expressions? You don't need a formal language to match a single character. It can be done with a simple string replace in your favorite language. Try str_replace('\\', '', $string)
in PHP, or myString.replace('\\', '')
in Javascript.
in python
re.sub(r'\\', '', text)
in c# it would be something like
Regex rgx = new Regex(@"\\");
string result = rgx.Replace(@"this backslash \ should go", "");
精彩评论