正则表达式删除方括号内的 html 标记



我使用正则表达式提取方括号内的内容:-

string.match(/[^[]]+(?=])/g)

但这不会提取方括号之间的字符串,例如

string= "[项目描述(仅适用于)]"

还有删除方括号内的 HTML 元素的正则表达式是什么

例:-

string="[<span>Description of project (only for)</span>]"

正则表达式后的输出应为"项目描述(仅适用于)"

  1. 匹配方括号中的所有内容。

  2. 使用回调函数替换此匹配内容中的 html 标记。

演示 : http://jsfiddle.net/uZRQt/

var matches = "This is <span>Outside content </span> and inside is [<span>Description of project (only for)</span>]".replace(/[.*?]/gi,function(match) {
    return match.replace(/</?[^>]*?>/gi,'');
});
alert(matches);

最新更新