views:

53

answers:

5

Hi There

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!

A: 

Double prefix with \ so become \\

Pentium10
A: 

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.

soulmerge
A: 

in python

re.sub(r'\\', '', text)
Tuomas Pelkonen
A: 

in c# it would be something like

Regex rgx = new Regex(@"\\");
string result = rgx.Replace(@"this backslash \  should go", "");
freddoo
@freddoo close. You need to escape the backslashes, so there would be 4 of them: "\\\\". Otherwise use the verbatim string symbol by prefixing the string with an `@` symbol. I've edited to include this.
Ahmad Mageed
+1  A: 

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?

Pat
it is a physical path c:\www\ << needed to remove the slashes
Gerald Ferreira