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
3 Answers
Reset to default 3You 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');
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744965985a4603681.html
评论列表(0条)