replace space with dash JavaScript

后端 未结 5 637
予麋鹿
予麋鹿 2020-12-17 23:29
var html = \"
\"+title+\"
\"; document.write(title.replace(/ /g,\"-\")); html+= \'

Detail

5条回答
  •  隐瞒了意图╮
    2020-12-18 00:27

    I find regex expressions commonly used in the replace function very hard to read - plus it's easy to forget to not quote the string you are searching for or to omit the /g to indicate a global replace. For doing something simple like replacing a space with a dash, using an easier to understand "split" followed by a "join" is just as fast.

    alert("this is a test".split(" ").join("-"));
    

    https://jsfiddle.net/n0u3aw5c/

提交回复
热议问题