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, "\\\\");
ASKER
oh wait please I cannot change my input string!!
ASKER
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\bol der\testco py
due because \t is NOT a slash followed by a t. It is seen as a tab character.
I know, but to test the regular expression you need to explicitly escape those slashes since your "test" string is NOT:
\\m-v94yl7juq66qy\test\bol
due because \t is NOT a slash followed by a t. It is seen as a tab character.
ASKER
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, "\\\\");
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, "\\\\");
var result = strAction.replace(/\b\\\b/
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Come on! isn't there a direct way to do that?!
ASKER
>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.
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, "\\\\");
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, "\\\\");
ASKER
!!!
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
ASKER
Masoudgh I will take solution if there is no one come up with an easy and direct way.
ASKER
> I will take solution
I mean I will take your solution
I mean I will take your solution
ASKER
>ah just change to vbscript and you won't have any of this hassle
Sorry this is not possible.
Sorry this is not possible.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
\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:
Open in new window