java 有没有什么方法可以在java中识别字符串是否包含HTML标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32065069/
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-11-02 19:34:27  来源:igfitidea点击:

Is there any method to identify whether a string contains HTML tags in java

javahtmlstring

提问by Neha S

Is there any predefined method stating whether a string contains HTML tags or characters in it?

是否有任何预定义的方法可以说明字符串中是否包含 HTML 标签或字符?

回答by Stanislav

You can try regular expressions, like this

你可以试试正则表达式,像这样

private static final String HTML_PATTERN = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
private Pattern pattern = Pattern.compile(HTML_PATTERN);

public boolean hasHTMLTags(String text){
    Matcher matcher = pattern.matcher(text);
    return matcher.find();
}

回答by Amit Bhati

Either Use regular expression to search or identify the HTML tags in String.

使用正则表达式搜索或识别字符串中的 HTML 标签。

boolean containsHTMLTag = stringHtml.matches(".*\<[^>]+>.*");

Or as Tim suggested use Jsoup like below:-

或者像 Tim 建议的那样使用 Jsoup,如下所示:-

String textOfHtmlString = Jsoup.parse(htmlString).text();
boolean containedHTMLTag = !textOfHtmlString.equals(htmlString);

回答by binaryakash

You should use find()

你应该使用 find()

private static final String HTML_TAG_PATTERN = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";

static Pattern htmlValidator = TextUtils.isEmpty(HTML_TAG_PATTERN) ? null:Pattern.compile(HTML_TAG_PATTERN);

public static boolean validateHtml(final String text){
    if(htmlValidator !=null)
      return htmlValidator.matcher(text).find();
    return false;
  }

回答by dZ.

Parsing String with Regex in order to search for HTML (in my case to prevent XSS attack related input) is not the proper way.

使用正则表达式解析字符串以搜索 HTML(在我的情况下是为了防止 XSS 攻击相关的输入)不是正确的方法。

A good way to achieve it is by using Spring HtmlUtils

实现它的一个好方法是使用 Spring HtmlUtils

Both are better explained already here,

两者都已经在这里更好地解释了,

https://codereview.stackexchange.com/questions/112495/preventing-xss-attacks-in-a-spring-mvc-application-controller

https://codereview.stackexchange.com/questions/112495/preventing-xss-attacks-in-a-spring-mvc-application-controller