Link to home
Start Free TrialLog in
Avatar of Abdu_Allah
Abdu_Allah

asked on

How to replace one backslash with two backslashes in string?

Hi, I'm trying to replace one backslash with two backslashes in the following code but it does not work, any suggestion?
var strAction = "\\m-v94yl7juq66qy\test\bolder\testcopy";
strAction.replace(/\\/gi, "\\\\");

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

the problem is your "input" string. In the same sense that \n is a newline character,
\t is a single tab character which is what is seen in \test (tab followed by est) and in \testcopy (tab followed by estcopy)
\b backspace character

You also need to "catch" the result; the replace method does not do inline replacement.

try this:
var strAction = '\\m-v94yl7juq66qy\\test\\bolder\\testcopy';
var result = strAction.replace(/\\/gi, "\\\\");
alert(result);

Open in new window

Avatar of Abdu_Allah
Abdu_Allah

ASKER

oh wait please I cannot change my input string!!
the input string will not be set explicitly, that just an example.
>>the input string will not be set explicitly, that just an example.
I know, but to test the regular expression you need to explicitly escape those slashes since your "test" string is NOT:
\\m-v94yl7juq66qy\test\bolder\testcopy

due because \t is NOT a slash followed by a t. It is seen as a tab character.
So how can I replace every one backslash with two backslashes without changing the input string?
with what is posted:
var result = strAction.replace(/\\/gi, "\\\\");
If you are trying NOT to replace the back-to-back slashes (like the ones you have at the beginning of your sample input, then use:
var result = strAction.replace(/\b\\\b/gi, "\\\\");
SOLUTION
Avatar of Masoudgh
Masoudgh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Come on! isn't there a direct way to do that?!
>If you are trying NOT to replace the back-to-back slashes (like the ones you have at the beginning of your sample input, then use:

Sorry I did not get your point, the input text can be anything.
>>the input text can be anything
OK, then what I posted originally will work. Assuming your input string is in a variable named strAction, then what you are after is:
var result = strAction.replace(/\\/g, "\\\\");

If you want strAction to contain the replaced string then use:
strAction = strAction.replace(/\\/g, "\\\\");
!!!
Abdu_Allah, there is solutions, why u don't pay attention?!
ah just change to vbscript and you won't have any of this hassle
Masoudgh I will take solution if there is no one come up with an easy and direct way.
> I will take solution

I mean I will take your solution
>ah just change to vbscript and you won't have any of this hassle

Sorry this is not possible.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial