1// You can use regex to do it in only one replace
2// Using the or: |
3var str = '#this #is__ __#a test###__';
4str.replace(/#|_/g,''); // result: "this is a test"
5// Or using the character class
6str.replace(/[#_]/g,''); // result: "this is a test"
1var str = '[T] and [Z] but not [T] and [Z]';
2var result = str.replace('T',' ').replace('Z','');
3console.log(result);