How to globally replace a character |(pipe) in a string? When I try to replace it with "so|me|str|ing".replace(/|/g, '-'), I get"-s-o-|-m-e-|-s-t-r-|-i-n-g-"
|
"so|me|str|ing".replace(/|/g, '-')
"-s-o-|-m-e-|-s-t-r-|-i-n-g-"
|has special meaning ( A|Bmeans "match A or B"), so you need to avoid it:
A|B
"so|me|str|ing".replace(/\|/g, '-');
|means OR, therefore, you need to escape as follows:\|
OR
\|
"so | me | str | ing".replace(/[|]/g, '-')
RegEx: https://www.regex101.com/
, . :
let output_delimiter ='|'; let str= 'Foo|bar| Test'; str.replace(new RegExp('[' + output_delimiter + ']', 'g'), '-') //should be 'Foo-bar- Test'