Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

regex - 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?

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can achieve that using this:

str = str.replace(/<brs*/?>/gi,' ');

This will match:

  • <br matches the characters <br literally (case insensitive)
  • s* match any white space character [ 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. All matches (don't return on first match)
  • i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

SNIPPET BELOW

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...