简体   繁体   English

全部替换<br> javascript中带有空格的标签

[英]Replace all <br> tag with space in javascript

Having trouble with very simple thing, How to properly replace all < br> and <br> in the string with the empty space?遇到问题,很简单的事情,如何正确地替换所有< 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);
}

You can achieve that using this:你可以使用这个来实现:

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

This will match:这将匹配:

  • <br matches the characters <br literally (case insensitive) <br字面上匹配字符<br (不区分大小写)
  • \\s* match any white space character [\\r\\n\\t\\f ] \\s*匹配任何空白字符[\\r\\n\\t\\f ]
  • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]量词: *在零次和无限次之间,尽可能多次,按需回馈[贪婪]
  • \\/? matches the character / literally匹配字符/字面意思
  • Quantifier: ?量词: ? Between zero and one time, as many times as possible, giving back as needed [greedy]在零到一次之间,尽可能多次,按需回馈[贪婪]
  • > matches the characters > literally >匹配字符>字面意思
  • g modifier: global. g修饰符:全局。 All matches (don't return on first match)所有比赛(不要在第一场比赛中返回)
  • i modifier: insensitive. i修饰符:不敏感。 Case insensitive match (ignores case of [a-zA-Z] )不区分大小写的匹配(忽略[a-zA-Z]大小写)

SNIPPET BELOW下面的片段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM