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

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

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

10条回答
  •  温柔的废话
    2020-11-28 22:57

    Without using regex (though I would only do this if the search string is user input):

    var str = 'Hello/ world/ this has two slashes!';
    alert(str.split('/').join(',')); // alerts 'Hello, world, this has two slashes!' 
    

提交回复
热议问题