How to replace special characters except /s? in nodejs?

I have a initialmsg ,

 let initialmsg = "zavio, invitation sent to {#arg#} {#arg#} &academy!"
 let msg = "zavio,\s?invitation\s?sent\s?to\s?.{0,2} .{0,2} &academy!"

After manipulation using regex it has been converted in to msg. Now I need to remove special characters in the sentence except \s?. How can I write the condition ?

 var splitters= /{#arg#}|(|[!-\/:-@[-`{-~])/gi;
 initialmsg= initialmsg.replace(/(^|.)\s($|.)/g, (x, g1, g2) => (x == "} {" ? x : g1 + "\\s?" + g2));
 msg= initialmsg.replace(splitters, (x,y) => y ? "" : ".{0,2}");

which produces the above msg.

  let msg = "zavio,\s?invitation\s?sent\s?to\s?.{0,2} .{0,2} &academy!"


 If i try to include in the ternary operation, 

 msg = initialmsg.replace(splitters, (x,y,z) => y ? "" : x ? ".{0,2}" : "\\s");

it prdouces different result. Please help me, My final result should be,

   msg = "zavio\s?invitation\s?sent\s?to\s?.{0,2} .{0,2} academy"


Read more here: https://stackoverflow.com/questions/66271747/how-to-replace-special-characters-except-s-in-nodejs

Content Attribution

This content was originally published by sasii at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: