【问题标题】:Regex to remove html tag inside square brackets正则表达式删除方括号内的 html 标记
【发布时间】:2012-10-15 10:57:18
【问题描述】:

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

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

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

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

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

示例:-

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

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

【问题讨论】:

标签: javascript regex


【解决方案1】:
  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);

【讨论】:

  • 嘿,它不适用于 getcontent=
    [任务名称] [任务的员工月总数 ]
       
      &nbsp ;
       
      &nbsp ;
       
    在这我不想失去任何除了方括号内的标记之外的其他标记
  • @user1744639,它也适用于这个输入:jsfiddle.net/uZRQt/1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
  • 2016-07-28
相关资源
最近更新 更多