Create a Javascript RegExp to find opening tags in HTML/php template

后端 未结 2 580
温柔的废话
温柔的废话 2021-01-14 05:20

I\'m trying to write a Javascript HTML/php parser which would extract all opening tags from a HTML/php source and return the type of tag and attributes with their values whi

2条回答
  •  -上瘾入骨i
    2021-01-14 05:40

    Just make sure the last letter before the '>' is not a ?, using [^?]. No lookaheads or -behinds needed.

    <[a-zA-Z](.*?[^?])?>
    

    the parentheses and the last ? is to also match tags like .

    EDIT The solution didn't work for single character tags without attributes. So here is one that does:

    <[a-zA-Z]+(>|.*?[^?]>)
    

提交回复
热议问题