简体   繁体   English

删除领导和对立 <br> 从字符串

[英]remove leading and traling <br> from a string

I have <br> inserted into a string using some logic in typescript. 我已经使用打字稿中的某些逻辑将<br>插入到字符串中。 I wanted to remove the <br> at the begining and end of the string. 我想在字符串的开头和结尾处删除<br> Others should remain as it. 其他人应该保持原样。 What is the best way to achieve? 最好的实现方法是什么?

I achieved it using mystring.substring(mystring.indexOf('<br>'),mystring.lastIndexOf('<br>')) 我使用mystring.substring(mystring.indexOf('<br>'),mystring.lastIndexOf('<br>'))实现了它

Want to know if its the optimum way of doing this. 想知道它的最佳方法。

Its a little more complicated.. Should replace all the 它有点复杂..应该替换所有
in the starting of the string and ending of the string. 在字符串的开头和结尾。

<br><br>hello<br>user<br><br><br> should return hello<br>user

To do exactly that, you can use this (confirmed locally), 要做到这一点,您可以使用它(本地确认),

mystring.replace(/(^(<br>)+)|((<br>)+$)/g, '')

See this for more info. 请参阅以获取更多信息。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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