我需要一个正则表达式来去除字符串中的任何 BBCode。我有以下内容(以及带有标签的数组):
new RegExp('\\[' + tags[index] + '](.*?)\\[/' + tags[index] + ']');
Run Code Online (Sandbox Code Playgroud)
它可以很好地拾取 [tag]this[/tag] ,但在使用 [url= http://google.com]this[/url]时失败。
我需要改变什么?多谢。
小智 8
我遇到了这个线程,发现它有助于让我走上正轨,但这是我花了两个小时为 JavaScript 构建的终极线程(这是我的第一个正则表达式!),并经过测试,可以很好地处理疯狂的嵌套,甚至不正确的嵌套字符串,它就可以了!:
string = string.replace(/\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]/img, '');
Run Code Online (Sandbox Code Playgroud)
如果字符串 =“[b][color=blue][url=www.google.com]Google[/url][/color][/b]”,则新字符串将为“Google”。惊人的。
希望有人觉得有用,这是 Google 中“JavaScript RegEx strip BBCode”的最佳匹配;)
| 归档时间: |
|
| 查看次数: |
6941 次 |
| 最近记录: |