【问题标题】:Replace markdown tag around string with html tag用html标签替换字符串周围的markdown标签
【发布时间】:2021-03-20 00:32:36
【问题描述】:

我试图抓取两组双下划线之间的内容,并在两个下划线html标签之间替换它,但我的正则表达式不太正确。

const str = "__test__"
str.replace(/__(\wd+)__/g, "<u>$1<\/u>")

console.log(str)

预期输出:&lt;u&gt;test&lt;/u&gt;

【问题讨论】:

  • 我想你的意思是__(\w+)__ 和&lt;u&gt;$1&lt;/u&gt;
  • 或/__([^\W_]+(?:_[^\W_]+)*)__/g
  • @Thefourthbird 哎呀,我忘记了示例中的结束标签,这是为了转义(我认为)。

标签: javascript html regex markdown


【解决方案1】:
console.log(str.replace(/__(\w+)__/g, "<u>$1<\/u>"))

【讨论】:

  • 这本身就有效,所以我的问题必须在其他地方。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多