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

LVL 3
Abdu_AllahAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
>>!!!
What does that mean? If you are confused by this statement:
>>>>If you want strAction to contain the replaced string then use:
>>>>strAction = strAction.replace(/\\/g, "\\\\");

what I meant was that if you had:
str="Jello";
and wanted to replace the J with an H, after this is executed:
str.replace(/J/,"H");

str does NOT contain "Hello". The replace() method does NOT automatically save the "new value" on the variable original variable. Instead it works on a copy of the value of the variable, changes the J to an H on that copy and then returns that copy. That's why if you were to save the following as test.html, you will first see Hello, then Jello
<script>
str="Jello";
alert( "Replace Value: " + str.replace(/J/,"H") );//this shows the "modified value"
alert( "Original Value: " + str );//this shows that the original value is still in str
</script>

You cannot modify the value into str directly. If you want str to have the "new Value" after you execute the replace(), you will have to save the new value to it like this:
str = str.replace(/J/,"H");
<script>
str="Jello";
alert( "Replace Value: " + str.replace(/J/,"H") );//this shows the "modified value"
alert( "Original Value: " + str );//this shows that the original value is still in str
str = str.replace(/J/,"H");
alert( "New value in str: " + str );//this shows that str now has the new value
</script>

Having said that, it is not clear if you want strAction to contain the "new Value" - the value with the replaced backslashes. IF you do, all you need is the one liner I showed you:

strAction = strAction.replace(/\\/g, "\\\\");
0
 
hieloCommented:
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

0
 
Abdu_AllahAuthor Commented:
oh wait please I cannot change my input string!!
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Abdu_AllahAuthor Commented:
the input string will not be set explicitly, that just an example.
0
 
hieloCommented:
>>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.
0
 
Abdu_AllahAuthor Commented:
So how can I replace every one backslash with two backslashes without changing the input string?
0
 
hieloCommented:
with what is posted:
var result = strAction.replace(/\\/gi, "\\\\");
0
 
hieloCommented:
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, "\\\\");
0
 
MasoudghConnect With a Mentor Commented:
Try :

<script type="text/javascript">
function Replace(inputString, orgChar, newChar)
{
	var outputString = inputString;
	var intIndex = outputString.indexOf(orgChar);
	while (intIndex != -1){
		outputString = outputString.replace(orgChar, newChar)
		intIndex = outputString.indexOf(orgChar);
	}
	return outputString;
}
 
var str="\\m-v94yl7juq66qy\test\bolder\testcopy";
str = str.replace(/\\/ig,"\\\\");
 
var orgChars = ["\t","\b"];
var newChars = ["\\\\t","\\\\b"];
 
var i=0;
for(i=0; i<orgChars.length; i++)
{
	str = Replace(str, orgChars[i], newChars[i]);
}
 
alert(str);
 
</script>

Open in new window

0
 
Abdu_AllahAuthor Commented:
Come on! isn't there a direct way to do that?!
0
 
Abdu_AllahAuthor Commented:
>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.
0
 
hieloCommented:
>>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, "\\\\");
0
 
Abdu_AllahAuthor Commented:
!!!
0
 
MasoudghCommented:
Abdu_Allah, there is solutions, why u don't pay attention?!
0
 
DauheeCommented:
ah just change to vbscript and you won't have any of this hassle
0
 
Abdu_AllahAuthor Commented:
Masoudgh I will take solution if there is no one come up with an easy and direct way.
0
 
Abdu_AllahAuthor Commented:
> I will take solution

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

Sorry this is not possible.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.