Regular expression to get a class name from html

前端 未结 5 1088
温柔的废话
温柔的废话 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:15

    Regular expressions are not a good fit for parsing HTML. HTML is not regular.

    jQuery can be a very good fit here.

    var html = 'Your HTML here...';
    
    $('
    ' + html + '
    ').find('[class~="b"]').each(function () { console.log(this); });

    The selector [class~="b"] will select any element that has a class attribute containing the word b. The initial HTML is wrapped inside a div to make the find method work properly.

提交回复
热议问题