How to remove special character (:\) from string

Vinayak Nikam
Kilo Contributor

Hi All,

Need a script to remove special character specially ":\" from string. Due to this special characters my API is not working.

I have tried below scripts but not working please suggest.

 

var str = "F:\";
var test = str.replace(/[^\d\w]/gi, '');
gs.print("Str: " + str + "\ntest: " + test);

---------------------------------------------------------------------

var str = "F:\";
var test = str.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
gs.print("Str: " + str + "\ntest: " + test);

 

Thanks in Advance..

Vinayak

12 REPLIES 12

Chuck Tomasi
Tera Patron

Instead of removing them, how about escaping them? Normally \ indicates a special character follows (e.g. \t is a tab). If you want to represent a \, then it needs to be \\. Does your API accept that? If so, then it's a simple matter of replacing \ with \\ like:

var test = str.replace('\', '\\');

 

Sudhanshu Talw1
Tera Guru

str.replace(":\","");

 

Hope it helps

 

Thanks

Sudhanshu

Hi Sudhanshu,

 

Thanks for your reply, this is not working.

Chuck Tomasi
Tera Patron

If you really must remove the colon and the backslash, you'll need to do it in two steps. 

str.replace(':\', ''); fails because backslash escapes the second quote and creates a syntax issue.

str.replace(':\\', ''); // removes \, but not colon, so...

var v1 = str.replace(':', '');

var v2 = v1.replace('\\', '');