[英]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.我有一个字符串,在开头和结尾以及中间也有多个 br 标签。 But I want to remove all br tags which are in start of string or end of string.
但我想删除所有在字符串开头或字符串结尾的 br 标签。
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我尝试遵循正则表达式 tp 替换字符串
Input.replace(/^(<br( \/)?>)*|(<br( \/)?>)*$/, '')
But it is not working.但它不起作用。
I have tried a separate regex to remove all br at end it is working.我尝试了一个单独的正则表达式来删除所有 br 最后它正在工作。
Input.replace(/(<br \/>\s*)+$/, '')
How can I get similar regex to remove br tags from beginning also.我怎样才能获得类似的正则表达式来从一开始就删除 br 标签。 Or If I can write a combined regex.
或者如果我能写一个组合的正则表达式。
You can use following regex:您可以使用以下正则表达式:
/^(\s*<br( \/)?>)*|(<br( \/)?>\s*)*$/gm
see demo https://regex101.com/r/cH7kL2/1见演示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.请注意,由于您有一个多行字符串,因此您需要使用
m
标志来强制您的正则表达式引擎匹配每行开头的锚点。
Demo:演示:
Input.replace(/^(\s+<br( \/)?>)*|(<br( \/)?>\s)*$/gm, '')
Try this regex:试试这个正则表达式:
Input.replace(/^( |<br \/>)*(.*?)( |<br \/>)*$/,"$2");
here the fiddle http://jsfiddle.net/WBfxm/68/这里是小提琴http://jsfiddle.net/WBfxm/68/
如果你需要用 br 修剪(白色)空格(注意浏览器处理不同的 br 格式,如<br>, <br/>, <br /> and <br / >
使用: /^(\\s*<br\\s*\\/?\\s*>\\s*)*|(\\s*<br\\s*\\/?\\s*>\\s*)*\\s*$/g
。请参阅此处的示例https:// regex101.com/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)]>
试试这个
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修改后的代码应清除字符串前面或末尾的所有空格和
<br>
或<br />
标签
here is working example: http://www.rubular.com/r/rHYW4DqJKT (Modified)这是工作示例: http : //www.rubular.com/r/rHYW4DqJKT (修改)
Edit: actually what you are missing is to give the replaced value to the original variable like so:编辑:实际上您缺少的是将替换值赋予原始变量,如下所示:
Input = Input.replace(/^(\ ?<br( \/)?>\ ?)+|(\ ?<br( \/)?>\ ?)+$/, '');
这个对我有用
this.htmlToPreview = payload.parsedHTML.replace(/<br\s*\/?>/gm, '');
This will replace each occurrence within the inputValue这将替换 inputValue 中的每个出现
const regexBr = new RegExp('<br/>', 'gi'); const outputValue = inputValue.replace(regexBr, ' ');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.