【问题标题】:regex javascript cut text between tags正则表达式 javascript 在标签之间剪切文本
【发布时间】:2022-11-18 15:56:38
【问题描述】:

当我在 [<] 中添加 ^[^<] 我的正则表达式不能正常工作请告诉我如何在标签之间剪切文本

const text = "<div>HellO</div>"
const regexp = "[^>]+[aA-zZ]+[<]"
console.log(text.match(regexp))

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    const text = "<div>HellO</div>"
    const regexp = ">([a-zA-Z0-9]+)<"
    console.log(text.match(regexp).pop())

    【讨论】:

      【解决方案2】:

      你可以使用组匹配来做到这一点Regex Demo

      ([^>]+[aA-zZ]+)[<]
      

      const text = "<div>HellO</div>"
      const regexp1 = /([^>]+[aA-zZ]+)[<]/
      console.log(text.match(regexp1)[1])
      
      const regexp2 = />(w+)</
      console.log(text.match(regexp2)[1])

      【讨论】: