Regular expression to get a class name from html

前端 未结 5 1085
温柔的废话
温柔的废话 2021-01-13 09:39

I know my question might look like a duplication for this question, but its not
I am trying to match a class name inside html text that comes from the

5条回答
  •  礼貌的吻别
    2021-01-13 10:16

    This may not be a solution for you, but if you aren't set on using a full regex match, you could do (assuming your examples are representative of the data you will be parsing) :

    function hasTheClass(html_string, classname) {
        //!!~ turns -1 into false, and anything else into true. 
        return !!~html_string.split("=")[1].split(/[\'\"]/)[1].split(" ").indexOf(classname);
    }
    
    hasTheClass("
    ", 'b'); //returns true

提交回复
热议问题