Unable to replace backward slash "\" from a string

Arpita14
Kilo Explorer

Hi All, 

 

I am trying to split/replace a string that has backward slash"\" but unable to get the desired result.

var strVal = "Au\Group\This group"
var newStr = strVal.replace(/\\/g,"/"); [ or var newStr = strVal.split("\'");]

gs.info(newStr)

 

**Script completed in scope : AuGroupThis group

Is there any other  way to achieve this. Any advise/inputs will be of great help.

 

Thank you.

 

 

6 REPLIES 6

harshinielath
Tera Expert

I find the code but try this way and see:

// Replace any '\' in str1 with '/' and store the result in str2
re2 = new RegExp("\\\\","g"); 
str2 = str1.replace(re2,"/");

I have tried this and it does not work. It returns a single string.

Greg42
Mega Guru
Hi Arpita,
var strVal = "Au\Group\This group"; <-- backslashed here are escaped straight away.
You will have to have double backslashes to make it work.
Try this:
var strVal = "Au\Group\This group";
strVal == "AuGroupThis group"; <-- this will evaluate to true.
This will work:
var strVal = "Au\\Group\\This group";
strVal.split("\\");
Wherever you get the data it will have to come in an escaped form not to be escaped.
Regards
Greg

Hi Greg, 

 

I see that the solution works if there are double backslashes, but i cant make any changes to the input string to have double backslash .

so is there no other way to remove a singe instance of backslash?

 

Thank you,