Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of regular expression matching closed HTML tag

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the regular expression matching closed HTML tag example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Any complex regular expression is composed of simple sub-expressions. In order to write complex regular expressions, on the one hand, we need to have the ability to simplify complex regular expressions, on the other hand, we need to think about problems from the perspective of regular engine. About the principle of regular engine, it is recommended that "Mastering Regular Expression" is called "proficient in regular expressions" in Chinese. It's a good book.

OK, first identify the problem we want to solve-- find the innerHTML of the tag of a particular id from a piece of Html text.

The biggest difficulty here is that Html tags support nesting, how can you find the closed tags corresponding to the specified tags?

We can think of it this way, first match the first starting tag, suppose it is div (?) * >) | (?) |. *) *

If you only want to match the div tag, you can use the following regular expression:

] *? > ((?) * >) | (?) |. *) *

Yes, you can modify the div to any HTML tag you want to match.

If you want to match multiple HTML tags at the same time, you can use the following regular expression:

] *? > ((?) * >) | (?) |. *) *

You can continue to add more tags to match

If you want to match tags that contain ID, you can use the following regular expression:

] *\ s [iI] [dD] = (? ["']) footer (? (Quote)\ k) [^ >] *? (/ > | > ((?) * >) |. *)

This regular matches any HTML tag whose id is footer

The editor added:

Regular\ k

You can also specify the group name of the subexpression yourself. To specify the group name of a subexpression, use the syntax (?\ W +) (or replace the angle brackets with'OK: (? 'Word'\ w +)), so that the group name of\ w + is specified as Word. To backreference the contents captured by this group, you can use\ k, so the previous example can also be written like this:\ b (?\ w +)\ b\ s +\ k\ b.

Yes, but it is used in conjunction with it. See below for details:

Specifies the group name of the subexpression. To specify the group name of a subexpression, use the syntax (?\ W +) (or replace the angle brackets with'OK: (? 'Word'\ w +)), so that the group name of\ w + is specified as Word. To backreference the contents captured by this group, you can use\ k, so the previous example can also be written like this:\ b (?\ w +)\ b\ s +\ k\ b.

The above is all the content of the article "sample Analysis of regular expression matching closed HTML tags". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report