Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if String contains html data?

Tags:

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 formatting.

like image 718
Joe Avatar asked Jun 16 '10 09:06

Joe


People also ask

How do I check if a string contains HTML?

test. bind(/(<([^>]+)>)/i); It will basically return true for strings containing a < followed by ANYTHING followed by > .

How do I check if a string contains text?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do you check if a string contains a character?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.

How do you check if a string contains a character JavaScript?

In JavaScript, includes() method determines whether a string contains the given characters within it or not. This method returns true if the string contains the characters, otherwise, it returns false.


2 Answers

I know this is an old question but I ran into it and was looking for something more comprehensive that could detect things like HTML entities and would ignore other uses of < and > symbols. I came up with the following class that works well.

You can play with it live at http://ideone.com/HakdHo

I also uploaded this to GitHub with a bunch of JUnit tests.

package org.github;  /**  * Detect HTML markup in a string  * This will detect tags or entities  *  * @author [email protected] - David H. Bennett  *  */  import java.util.regex.Pattern;  public class DetectHtml {     // adapted from post by Phil Haack and modified to match better     public final static String tagStart=         "\\<\\w+((\\s+\\w+(\\s*\\=\\s*(?:\".*?\"|'.*?'|[^'\"\\>\\s]+))?)+\\s*|\\s*)\\>";     public final static String tagEnd=         "\\</\\w+\\>";     public final static String tagSelfClosing=         "\\<\\w+((\\s+\\w+(\\s*\\=\\s*(?:\".*?\"|'.*?'|[^'\"\\>\\s]+))?)+\\s*|\\s*)/\\>";     public final static String htmlEntity=         "&[a-zA-Z][a-zA-Z0-9]+;";     public final static Pattern htmlPattern=Pattern.compile(       "("+tagStart+".*"+tagEnd+")|("+tagSelfClosing+")|("+htmlEntity+")",       Pattern.DOTALL     );      /**      * Will return true if s contains HTML markup tags or entities.      *      * @param s String to test      * @return true if string contains HTML      */     public static boolean isHtml(String s) {         boolean ret=false;         if (s != null) {             ret=htmlPattern.matcher(s).find();         }         return ret;     }  } 
like image 194
David H. Bennett Avatar answered Nov 09 '22 00:11

David H. Bennett


You can use regular expressions to search for HTML tags.

like image 20
Tom Gullen Avatar answered Nov 09 '22 00:11

Tom Gullen