javascript 正则表达式:从“内容”中排除 html 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22259540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:47:32  来源:igfitidea点击:

Regular Expression: exclude html tags from "content"

javascriptregex

提问by Renato Galvones

One friend asked me this and as my knowledge on RegExp is not so good yet here I am.

一位朋友问我这个问题,因为我对 RegExp 的知识还不是很好,所以我在这里。

How can exclude the HTML tags from this string?

如何从这个字符串中排除 HTML 标签?

re<br>na<br>to<br>galvao

I've tried some RegExp but it didn't work as I was expecting.

我试过一些 RegExp 但它没有像我预期的那样工作。

(.*)<.*>(.*)

But this RegExp gets the first < and the last >.

但是这个 RegExp 得到第一个 < 和最后一个 >。

Any ideas?

有任何想法吗?

采纳答案by Crayon Violent

this is a quick way to do it:

这是一个快速的方法:

var content = "re<br>na<br>to<br>galvao";
content = content.replace(/<[^>]*>/g,'');

回答by davir

You could use a non-greedy match. According to the answer to this question, in javascript it is *?

您可以使用非贪婪匹配。根据这个问题的答案,在 javascript 中它是 *?

So, assuming this is the only problem with your regex, it should work with

因此,假设这是您的正则表达式的唯一问题,它应该适用

(.*?)<.*?>(.*?)

回答by donfuxx

Match all html tags with this regex:

用这个正则表达式匹配所有 html 标签:

 <("[^"]*?"|'[^']*?'|[^'">])*>

see demo here: http://regex101.com/r/fA0oT4

在此处查看演示:http: //regex101.com/r/fA0oT4