Manjay_TBAG
Manjay_TBAG

Reputation: 2245

Regex to remove all <br /> tags from beginning and end of a string in javascript

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 combined regex.

Upvotes: 2

Views: 9545

Answers (8)

This one worked for me

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

Upvotes: 0

Matias Moron
Matias Moron

Reputation: 24

This will replace each occurrence within the inputValue

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

Upvotes: -1

Bernd Buffen
Bernd Buffen

Reputation: 15057

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)]>

Upvotes: 1

Volodymyr Kulibaba
Volodymyr Kulibaba

Reputation: 21

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.com/r/ls9eoq/2

Upvotes: 2

Kasravnd
Kasravnd

Reputation: 107347

You can use following regex:

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

see demo https://regex101.com/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, '')

Upvotes: 8

Nik Chankov
Nik Chankov

Reputation: 6047

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.com/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

Upvotes: 0

Pavan Teja
Pavan Teja

Reputation: 3202

Try this

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

Upvotes: 0

Shailendra Sharma
Shailendra Sharma

Reputation: 6992

Try this regex:

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

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

Upvotes: 2

Related Questions