How can I globally replace the | (pipe) symbol in a string? When I try to replace it with \"so|me|str|ing\".replace(/|/g, \'-\'), I get \"-s-
|
\"so|me|str|ing\".replace(/|/g, \'-\')
\"-s-
In my case, the pipe was coming as an variable, so I couldn't use any of these solutions. Instead, You can use:
let output_delimiter ='|'; let str= 'Foo|bar| Test'; str.replace(new RegExp('[' + output_delimiter + ']', 'g'), '-') //should be 'Foo-bar- Test'