How to globally replace a forward slash in a JavaScript string?

后端 未结 10 2026
逝去的感伤
逝去的感伤 2020-11-28 22:05

How to globally replace a forward slash in a JavaScript string?

10条回答
  •  粉色の甜心
    2020-11-28 22:40

    You can create a RegExp object to make it a bit more readable

    str.replace(new RegExp('/'), 'foobar');
    

    If you want to replace all of them add the "g" flag

    str.replace(new RegExp('/', 'g'), 'foobar');
    

提交回复
热议问题