【问题标题】:how to use replace() method for a string just one time instead of mutliple times?如何对字符串只使用一次替换()方法而不是多次?
【发布时间】:2020-04-26 12:47:30
【问题描述】:

str 是一个 html 字符串(或包含完整 html 的字符串),但中间有一些我试图摆脱的不需要的代码。

到目前为止,这对我有用:

const contentClean = str.replace(/=0A/gm,"").replace(/(=09)/gm,"").replace(/(=\r\n)/gm,"").replace(/(3D)/gm,"");

如何在上面的代码中只编写一次 replace() 方法而不是 4 次?

【问题讨论】:

    标签: javascript string replace str-replace regexp-replace


    【解决方案1】:

    只需使用| 在每种可能性之间交替:

    const contentClean = str.replace(/=0A|=09|=\r\n|3D/gm, "");
    

    请注意,不需要捕获组。

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 2017-10-23
      • 2016-12-03
      • 2017-05-08
      • 2020-01-16
      • 2011-12-01
      • 2013-12-05
      相关资源
      最近更新 更多