求救,为什么这个正则不能去掉标签?

str=`【每日一题&nbsp;2021.12.17】<span lang="EN-US" style="font-size:14.0pt;font-family:&quot;Noto Sans&quot;,sans-serif;mso-bidi-font-family:\n等线;mso-bidi-theme-font:minor-fareast" data-mce-style="font-size: 14.0pt; font-family: 'Noto Sans',sans-serif; mso-bidi-font-family: 等线; mso-bidi-theme-font: minor-fareast;">一、<span style="font-size:14.0pt;font-family:等线;\nmso-ascii-font-family:&quot;Noto Sans&quot;;mso-fareast-theme-font:minor-fareast;\nmso-hansi-font-family:&quot;Noto Sans&quot;;mso-bidi-font-family:&quot;Noto Sans&quot;;color:black;\nmso-color-alt:windowtext;background:white" data-mce-style="font-size: 14.0pt; font-family: 等线; mso-ascii-font-family: 'Noto Sans'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Noto Sans'; mso-bidi-font-family: 'Noto Sans'; color: black; mso-color-alt: windowtext; background: white;">题目已知正整数n是两个不同的质数的乘积,试求出两者中较大的那个质数。[图片]`;

str.replace(/<.+?>/g, "");

我确定正则本身是没问题的,因为别的文本都正常去掉了标签,但就这个文本(我省略了很多)去不掉标签

我怀疑字符串中的 <> 不对,但是不知道啥问题啊

image.png

阅读 3.1k
3 个回答

正则表达式中的 . 不匹配换行符,你那个源数据里面有 \n

/<[^<>]+>/g

/<.+?>/sg
str.replace(/<[\s\S]+?>/g, "");
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题