JavaScript Non-regex Replace

后端 未结 4 1730
旧时难觅i
旧时难觅i 2021-02-19 08:08

Do any of the existing JavaScript frameworks have a non-regex replace() function, or has this already been posted on the web somewhere as a one-off function?

<
4条回答
  •  终归单人心
    2021-02-19 08:24

    I had exactly the same problem searching for a non-regex javascript string replace() method. My solution was to use a combination of split() and join():

    "some text containing regex interpreted characters: $1.00".split("$").join("£");
    

    which gives:

    "some text containing regex interpreted characters: £1.00"

    compare with replace():

    "some text containing regex interpreted characters: $1.00".replace(new RegExp("$"),"£")
    

    which bizarrely gives:

    "some text containing regex interpreted characters: $1.00£"

提交回复
热议问题