Solved

How to replace one backslash with two backslashes in string?

Posted on 2008-06-14
18
1,665 Views
Last Modified: 2008-06-15
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

0
Comment
Question by:Abdu_Allah
  • 9
  • 6
  • 2
  • +1
18 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21785233
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
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21785794
oh wait please I cannot change my input string!!
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21785808
the input string will not be set explicitly, that just an example.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21785996
>>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
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21786076
So how can I replace every one backslash with two backslashes without changing the input string?
0
 
LVL 82

Expert Comment

by:hielo
ID: 21786173
with what is posted:
var result = strAction.replace(/\\/gi, "\\\\");
0
 
LVL 82

Expert Comment

by:hielo
ID: 21786201
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
 
LVL 3

Assisted Solution

by:Masoudgh
Masoudgh earned 100 total points
ID: 21786263
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
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21786725
Come on! isn't there a direct way to do that?!
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21786732
>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
 
LVL 82

Expert Comment

by:hielo
ID: 21787371
>>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
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21787912
!!!
0
 
LVL 3

Expert Comment

by:Masoudgh
ID: 21787920
Abdu_Allah, there is solutions, why u don't pay attention?!
0
 
LVL 7

Expert Comment

by:Dauhee
ID: 21788280
ah just change to vbscript and you won't have any of this hassle
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21788533
Masoudgh I will take solution if there is no one come up with an easy and direct way.
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21788536
> I will take solution

I mean I will take your solution
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 21788537
>ah just change to vbscript and you won't have any of this hassle

Sorry this is not possible.
0
 
LVL 82

Accepted Solution

by:
hielo earned 400 total points
ID: 21788764
>>!!!
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now