How to remove special character (:\) from string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 06:47 AM
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
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 06:51 AM
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('\', '\\');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 06:57 AM
str.replace(":\","");
Hope it helps
Thanks
Sudhanshu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 09:39 PM
Hi Sudhanshu,
Thanks for your reply, this is not working.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 07:02 AM
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('\\', '');