简体   繁体   中英

How to remove line breaks from a string, but only between certain tags?

I have already found How to remove all line breaks from a string but instead of a simple string I have HTML tags within. My goal is to remove all line breaks only for the string parts that are within <p> tags.

Example String:

var str = `<h1>Headline do not touch me, nor the line break</h1>

<p>This is 
a test string, please 
put me on one line.</p>`;

Should become:

var str = `<h1>Headline do not touch me, nor the line break</h1>

<p>This is a test string, please put me on one line.</p>`;

What would be the JS code / Regex to achieve this?


Note: There are several p tags in my strings in production.

I found this on another website but are not able to modify it accordingly: str=str.replace(/(<[^>]+>)/g,function(w){return w.replace(/(\\r\\n|[\\r\\n])/g,' ')});

You can use two regex one to replace new line inside tags and another to remove between tags

  1. <[^>]+>[\\s\\S]+?<\\/[^>]+> --> to remove new line inside tags

在此处输入图片说明

  1. (<\\/[^>]+>)\\n+(?=<[^>]+>) --> to remove new line between tags

在此处输入图片说明

 let str = `<h1>Headline One Do not touch</h1> <p> This is a test string, please put me on one line. </p> <p> some text </p> ` let output = str.replace(/<[^>]+>[\\s\\S]+?<\\/[^>]+>/g, m => m.replace(/\\n+/g, '')) let final = output.replace(/(<\\/[^>]+>)\\n+(?=<[^>]+>)/g,'$1\\n') console.log(final) 

Below code works if you have only one p tag in your string.

var str = `<h1>Headline do not touch me, nor the line break</h1>

<p>
This is 
a test string, please 
put me on one line.
</p>`;

var str = str.substring(0, str.indexOf('<p>')) + str.slice(str.indexOf('<p>'), str.indexOf('</p>')).replace(/(\r\n|[\r\n])/g,' ') + str.substring(str.indexOf('</p>'), str.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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