替换所有< br>标记与javascript中的空格

问题描述:

麻烦很简单,
如何正确替换所有<带有空格的字符串中的br> < br>

这是我正在尝试使用的,但我收到相同的字符串。:

This is what I'm trying to use, but I'm receiving the same string.:

var finalStr = replaceAll(replaceAll(scope.ItemsList[i].itemDescr.substring(0, 27), "<", " "), "br>", " ");
function replaceAll(str, find, replace) {
    return str.replace(new RegExp(find, 'g'), replace);
}


你可以用这个实现:

str = str.replace(/<br\s*\/?>/gi,' ');

这将匹配:


  • < br 匹配字符< br 字面意思(不区分大小写)

  • \ * * 匹配任何空格字符 [\\\\\\\\\\ code>


    • 量词: * 在零和无限次之间,尽可能多次,根据需要回馈[贪心]

    • <br matches the characters <br literally (case insensitive)
    • \s* match any white space character [\r\n\t\f ]
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]

      • 量词:在零到一次之间,尽可能多次,根据需要回馈[贪心]

      • Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]

      var str = "This<br />phrase<br>output<BR/>will<Br/>have<BR>0 br";
      str = str.replace(/<br\s*\/?>/gi, ' ');
      console.log(str)