Reputation: 32949
I have a multi-line string in javascript as follows:
"<p> </p>
<p> </p>
<p> </p>
<p> </p>
"
I am trying to remove the extra instances of <p> </p>
, so I am trying to use the following regex.
var content = getContent() // This returns the string mentioned above.
content = content.replace(/(<p> <\/p>)+/g, "<p> <\/p>");
But this does not seem to work. Most probably because the string is a multi-line string ? I have tried the above regex with a single line string and it works fine. After reading a little more documentation about javascript regex, I realized that we need to use the m
modifier to deal with multi-line strings. So I changed the regex to:
content = content.replace(/(<p> <\/p>)+/gm, "<p> <\/p>"); // note the `m` modifier
But it still does not work. any clues ?
Upvotes: 3
Views: 463
Reputation: 785156
se this replace:
content = content.replace(/(<p> +<\/p>\n*)+/g, "<p> </p>");
/=> <p> </p>
Upvotes: 0
Reputation: 382150
You should replace all spaces (including new lines) :
content = content.replace(/(<p>\s*<\/p>\s*)+/g, "<p> <\/p>");
Upvotes: 2