How to find if String contains html data?

后端 未结 7 2338
遇见更好的自我
遇见更好的自我 2020-12-15 18:13

How do I find if a string contains HTML data or not? The user provides input via web interface and it\'s quite possible he could have used either a simple text or used HTML

相关标签:
7条回答
  • 2020-12-15 19:12

    I'm using regex:

    [\S\s]*\<html[\S\s]*\>[\S\s]*\<\/html[\S\s]*\>[\S\s]*

    So in JAVA it looks like:

    text.matches("[\\S\\s]*\\<html[\\S\\s]*\>[\\S\\s]*\\<\\/html[\\S\\s]*\\>[\S\s]*");

    It should match any correct (as well as some incorrect) XML file that contains somewhere an "html" element. So there might be false positives.

    Edit:

    Since I have posted that, I have removed the last part with html element closing, as I found some websites don't use it. (?!) So in case, you prefer false positives to false negatives, I encourage to do that!

    0 讨论(0)
提交回复
热议问题