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

jquery - Regex to remove all <br > tags from beginning and end of a string in javascript - Stack Overflow

programmeradmin3浏览0评论

I have a string with multiple br tags in beginning and end and in between also. But I want to remove all br tags which are in start of string or end of string.

Input : <br /> <br /> <br /> 
        <br /> <br />This is <br /> Test <br /> string. <br /> <br /> <br /> 

Output should be - This is <br /> Test <br /> string. 

I tried following regex tp replace string

Input.replace(/^(<br( \/)?>)*|(<br( \/)?>)*$/, '')

But it is not working.

I have tried a separate regex to remove all br at end it is working.

Input.replace(/(<br \/>\s*)+$/, '')

How can I get similar regex to remove br tags from beginning also. Or If I can write a bined regex.

I have a string with multiple br tags in beginning and end and in between also. But I want to remove all br tags which are in start of string or end of string.

Input : <br /> <br /> <br /> 
        <br /> <br />This is <br /> Test <br /> string. <br /> <br /> <br /> 

Output should be - This is <br /> Test <br /> string. 

I tried following regex tp replace string

Input.replace(/^(<br( \/)?>)*|(<br( \/)?>)*$/, '')

But it is not working.

I have tried a separate regex to remove all br at end it is working.

Input.replace(/(<br \/>\s*)+$/, '')

How can I get similar regex to remove br tags from beginning also. Or If I can write a bined regex.

Share Improve this question asked Aug 26, 2015 at 11:38 Manjay_TBAGManjay_TBAG 2,2453 gold badges24 silver badges45 bronze badges 1
  • replace (<br \/>\s)+ – Raghavendra Commented Aug 26, 2015 at 11:42
Add a ment  | 

8 Answers 8

Reset to default 8

You can use following regex:

/^(\s*<br( \/)?>)*|(<br( \/)?>\s*)*$/gm

see demo https://regex101./r/cH7kL2/1

Note that since you have a multi-line string you need to use m flag which forced your regex engine to match the anchors at the start of each line.

Demo:

Input.replace(/^(\s+<br( \/)?>)*|(<br( \/)?>\s)*$/gm, '')

Try this regex:

Input.replace(/^( |<br \/>)*(.*?)( |<br \/>)*$/,"$2");

here the fiddle http://jsfiddle/WBfxm/68/

In case you need trim (white) spaces with br (pay attention browser handle different br format like <br>, <br/>, <br /> and <br / > use that: /^(\s*<br\s*\/?\s*>\s*)*|(\s*<br\s*\/?\s*>\s*)*\s*$/g. see example here https://regex101./r/ls9eoq/2

you can use a regex like this:

SELECT REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2');

Sample

MariaDB [(none)]> SELECT REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2');
+------------------------------------------------------------------------+
| REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2') |
+------------------------------------------------------------------------+
| Hello<br>world                                                         |
+------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT REGEXP_REPLACE('Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2');
+--------------------------------------------------------------------+
| REGEXP_REPLACE('Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2') |
+--------------------------------------------------------------------+
| Hello<br>world                                                     |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT REGEXP_REPLACE('<br>Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2');
+--------------------------------------------------------------------+
| REGEXP_REPLACE('<br>Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2') |
+--------------------------------------------------------------------+
| Hello<br>world                                                     |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT REGEXP_REPLACE('Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2');
+----------------------------------------------------------------+
| REGEXP_REPLACE('Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2') |
+----------------------------------------------------------------+
| Hello<br>world                                                 |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

Try this

Input.replace(/^[(<br( \/)> )|(<br( \/)>)]*|[( <br( \/)>)|(<br( \/)>)]*$/g, '');

It should be:

Input.replace(/^(\ ?<br( \/)?>\ ?)+|(\ ?<br( \/)?>\ ?)+$/, '');

The modified code should clear all spaces and <br> or <br /> tags infront or at the end of the string

here is working example: http://www.rubular./r/rHYW4DqJKT (Modified)

Edit: actually what you are missing is to give the replaced value to the original variable like so:

Input = Input.replace(/^(\ ?<br( \/)?>\ ?)+|(\ ?<br( \/)?>\ ?)+$/, '');

Working example in jsfiddle

This one worked for me

this.htmlToPreview = payload.parsedHTML.replace(/<br\s*\/?>/gm, '');

This will replace each occurrence within the inputValue

const regexBr = new RegExp('<br/>', 'gi');
const outputValue = inputValue.replace(regexBr, ' ');

发布评论

评论列表(0)

  1. 暂无评论