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

Hi Chuck,

 

I have tested this but unfortunately not working. my code is as below. correct if i missed anything.

 

var str = "F:\";
var v1 = str.replace(':', '');
var v2 = v1.replace('\\', '');
gs.info("Str: " + str + "\nreplaced str: " + v2);

 

Thanks,

Vinayak 

The issue with this script is on line 1. The JavaScript interpreter is reading it as an unterminated string because \" is actually making the quote character part of the string, not a terminator.

You can change it to either of these two:

var str = "F:\\"; // creates F:\
var str = "F:\""; // creates F:"

 

Hi, Im trying to remove special characters from description on RITM level, after copied all variables to description (BR):

(function executeRule(current, previous /*null when async*/) {

var desc = current.variables.u_description;


current.description = 'Request Item : ' + current.cat_item.getDisplayValue() + '\nRequest Number: ' + '\n' + current.number + '\nRequest Type: ' + current.variables.request_type.getDisplayValue() +'\nShort Description: ' + current.variables.u_short_description + '\nDescription: ' + desc + '\nJustification: ' + current.variables.justification + '\nCost Approval: ' + current.variables.cost_approval.getDisplayValue();



})(current, previous);

 

so far didn't manage to succeed 

Rishabh Jha
Mega Guru

Hi @Vinayak Nikam ,

Your str variable declaration at the first line with the initial value "F:\" wouldn't work, because it's not a proper string literal. It has to be escaped with "\" to be able to apply any function to it.

So, if you'd try the below snippet, that should give you the desired result. You can use the global replace link below to replace it everywhere in the string:

var str = "F:\\";
var test = str.replace(/:\\/g, '');
gs.info("Str: " + str + "\nreplaced str: " + test);

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

Hi Rishabh,

 

your code is working but there is only one blackslash in my string.