最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Removing <p><br><p> tag from starting and ending of a content in JavaScript - Stack Ov

programmeradmin1浏览0评论

I am looking for a javascript regex through which I could remove the <p><br><p> tag from my content.

for example:

Below one is my content

<p><br></p>
<p>function removes whitespace or other predefined characters from the right side of a string.</p>
<p><br></p>
<p><br/><p>

and I am looking for this

<p>function removes whitespace or other predefined characters from the right side of a string.</p>

I am using this code but it is not working

function rtrim(str) {
  if(!str) return str;
  return str.replace(/\s+$/g, '');
}

console.log(rtrim(string));

I am looking for a javascript regex through which I could remove the <p><br><p> tag from my content.

for example:

Below one is my content

<p><br></p>
<p>function removes whitespace or other predefined characters from the right side of a string.</p>
<p><br></p>
<p><br/><p>

and I am looking for this

<p>function removes whitespace or other predefined characters from the right side of a string.</p>

I am using this code but it is not working

function rtrim(str) {
  if(!str) return str;
  return str.replace(/\s+$/g, '');
}

console.log(rtrim(string));
Share Improve this question asked Dec 19, 2019 at 12:49 Rohit SharmaRohit Sharma 1694 silver badges20 bronze badges 5
  • Do you want to remove <p><br></p> or <p><br/><p> or <p><br><p>? ;) They're all different, and you need to be quite specific with regex – Blundering Philosopher Commented Dec 19, 2019 at 12:52
  • 1 @BlunderingPhilosopher Hi, I am trying to remove <p><br></p> only from the starting and ending of the contents. – Rohit Sharma Commented Dec 19, 2019 at 12:53
  • Is it required that you use a RegExp? Or is it ok to split and filter the string? – Keff Commented Dec 19, 2019 at 12:57
  • Ok, so you don't care about <br> vs <br/>? Also when you say "from the starting and ending of the contents", you mean you only care to remove it from the first line and last line? Or you want to remove it anywhere it shows up in the entire content? – Blundering Philosopher Commented Dec 19, 2019 at 12:58
  • any number of <p><br><p> s from starting and ending – Rohit Sharma Commented Dec 19, 2019 at 13:06
Add a ment  | 

3 Answers 3

Reset to default 3

You want to remove the HTML Linebreaks <br/> and its surrounding paragraph element <p> and not whitespaces, which you do with your current regex.

\s+ matches any whitespace character (equal to [\r\n\t\f\v ])

This should be the correct regex in your case <p><br[\/]?><[\/]?p>

function rtrim(str) {
  if(!str) return str;
  return str.replace(/<p><br[\/]?><[\/]?p>/g, '');
}

console.log(rtrim("<p><br></p><p>function removes whitespace or other predefined characters from the right side of a string.</p><p><br></p><p><br/><p>"));

I used <br[\/]?> to make sure both linebreaks with and without a forward slash will match.

Instead of replacing <p><br></p>, You can extract only <p>some text</p>.

For example like below,

let a = "<p><br></p><p>function removes whitespace or other predefined characters from the right side of a string.</p><p><br></p><p><br/><p>";
a = /(<p>[^<>].*[^<>]<\/p>)/g.exec(a)[0];
console.log(a); // "<p>function removes whitespace or other predefined characters from the right side of a string.</p>"

If you need specifically a RegExp I would suggest the answer posted by Red.

If you are not required to use a RegExp, you could split by line and filter that string, then join it again,
although this example will only work if they are separated by \n:

function rtrim(str) {
  if(!str) return str;
  return str
    .split('\n')
    .filter((s) => s === '<p><br></p>')
    .join('\n');
}


发布评论

评论列表(0)

  1. 暂无评论