简体   繁体   English

正则表达式删除所有<br /> javascript中字符串开头和结尾的标签

[英]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( \/)?>\ ?)+$/, '');

Working example in jsfiddle jsfiddle 中的工作示例

这个对我有用

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.

相关问题 如何删除 <br> 在使用正则表达式的javascript中的字符串的开头和结尾? - How to remove <br> at the beginning and at the end of a string in javascript using regex? 去掉 <ul> 字符串开头和结尾的标签(JavaScript或PHP) - Remove <ul> tags from beginning and end of string (JavaScript or PHP) 需要 Javascript 正则表达式删除一个“<br> “标签来自任意数量的连续”<br> " 字符串中的标签 - Need Javascript Regex to Remove one "<br>" tag from any number of consecutive "<br>" tags in a string 移除所有<br>字符串中的标签 - 未按预期工作 - Remove all <br> tags from within a string – not working as expected 我如何删除所有<br>字符串中的标签 - How do I remove all the <br> tags from a string 使用jQuery或javascript从网页中存在的所有文本框的字符串开头和结尾删除空格 - Remove whitespace from beginning and end of string of all textbox present in a webpage using jQuery or javascript 移除所有 <br> 从一个字符串 - remove all <br> from a string Javascript 更换增加几<br> 's 到字符串的开头和结尾 - Javascript Replace adds several <br />'s to beginning and end of string Javascript正则表达式删除开头和结尾的特定字符串 - Javascript regex to remove Specific strings in beginning and end 正则表达式删除字符串开头和结尾的某些字符 - regex to remove certain characters at the beginning and end of a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM