How can I replace special character with empty string

Alon Grod
Tera Expert

Hi,

 

Im getting two strings in my log:

 

1) '22122b37c611228400f9ff91c857581d]'

2) '[aaccc971c0a8001500fe1ff4302de101'

 

how can I replace the special character '[' and ']' in each case. Im trying to use:

sys_id = str.replace([, '');

sys_id = str.replace(], '');

 

but its not working

1 ACCEPTED SOLUTION

Matt102
Giga Guru

Hi,

For me, validated in script background:

 

sys_id = str.replace(/[\[\]]/, '');

 

 
Validation was

 

var str1 = '22122b37c611228400f9ff91c857581d]';
var str2 = '[aaccc971c0a8001500fe1ff4302de101';
var newStr;

newStr = str1.replace(/[\[\]]/, '');
gs.info(newStr);
newStr = str2.replace(/[\[\]]/, '');
gs.info(newStr);

 

 

hth,matt

View solution in original post

2 REPLIES 2

Matt102
Giga Guru

Hi,

For me, validated in script background:

 

sys_id = str.replace(/[\[\]]/, '');

 

 
Validation was

 

var str1 = '22122b37c611228400f9ff91c857581d]';
var str2 = '[aaccc971c0a8001500fe1ff4302de101';
var newStr;

newStr = str1.replace(/[\[\]]/, '');
gs.info(newStr);
newStr = str2.replace(/[\[\]]/, '');
gs.info(newStr);

 

 

hth,matt

Hi,

I now realise my answer may have been something of a partial solution, in that it will only replace one instance of the characters specified, to remove multiple instances of ][ from a string I believe this global replace may be more appropriate:

sys_id = str.replace(/[\[\]]/g, '');

hth,matt