Home >Web Front-end >JS Tutorial >Detailed explanation of backreference of regular expressions_regular expressions
This article mainly introduces Regular expressionsBacktracking of learning tutorialsReferencebackreference, combined with examples, a detailed analysis of the concept, function and implementation skills of backreference, friends in need You can refer to the following example
This article describes the regular expression backreference backreference. Share it with everyone for your reference, the details are as follows:
In all examples, the regular expression matching results are included between [and] in the source text. Some examples will be implemented using Java. If It is the usage of regular expressions in Java itself, which will be explained in the corresponding place. All java examples are tested under JDK1.6.0_13.
1. Problem introduction
A problem of matching title tags (H1-H6) in HTML pages:
Text:
<body> <h1>Welcome to my page</H1> Content is pided into twosections:<br> <h2>Introduction</h2> Information about me. <H2>Hobby</H2> Information about my hobby. <h2>This is invalid HTML</h3> </body>
Regular expression: bec64cdfd3b18ddf17d11124d921cd9d.*?c0d08c9e09935101747a568bd8607d7d
Result:
6c04bd5ca3fcae76e30b72ad730ca86d
【4a249f0d628e2318394fd9b75b4636b1Welcome to my page921186cad5e55f1481a45fdcd6c30f6e】
Content is pided into two sections:0c6dc11e160d3b678d68754cc175188a
【c1a436a314ed609750bd7c7d319db4daIntroduction2e9b454fa8428549ca2e64dfac4625cd】
Information about me.
【1b7994ba0f794e1b49742a6367e9e0c6Hobbyebe8950f0835d9653ff486769499854f】
Information about my hobby.
【c1a436a314ed609750bd7c7d319db4daThis is invalid HTML39528cedfa926ea0c01e69ef5b2ea9b0】
36cc49f0c466276486e50c850b7e4956
Analysis: Pattern248fd9e9bb80e557cb3d346d628664f7 matches the opening tag of any first-level title, and is not case-sensitive. In this example, it matches 4a249f0d628e2318394fd9b75b4636b1, c1a436a314ed609750bd7c7d319db4da, c0d08c9e09935101747a568bd8607d7d Matched 473f0a7621bec819994bb5020d29372a, 2e9b454fa8428549ca2e64dfac4625cd, 39528cedfa926ea0c01e69ef5b2ea9b0; the lazy metacharacter is used here to match the text in the tag, otherwise it will match the tag starting from the first one to the content between the last closing tags. However, it can be seen from the results that an invalid tag is also matched, namely c1a436a314ed609750bd7c7d319db4da39528cedfa926ea0c01e69ef5b2ea9b0, and they cannot be matched at all. To solve this problem, you need to use backreference.
2. Backreference matching
Backreference means that the second half of the pattern refers to the subexpression defined in the first half. As for the use, division and reference of subexpressions, they have been introduced before. Now let’s solve the previous example:
Text:
<body> <h1>Welcome to my page</H1> Content is pided into twosections:<br> <h2>Introduction</h2> Information about me. <H2>Hobby</H2> Information about my hobby. <h2>This is invalid HTML</h3> </body>
Regular expression: 2b0cba08e60dd5488139acd8c53df8f7.*?2f213dbbacf53066911c7f9c5bc8505c
Result:
##6c04bd5ca3fcae76e30b72ad730ca86d
【4a249f0d628e2318394fd9b75b4636b1Welcome to my page921186cad5e55f1481a45fdcd6c30f6e】
Content is pided into two sections:0c6dc11e160d3b678d68754cc175188a
【c1a436a314ed609750bd7c7d319db4daIntroduction2e9b454fa8428549ca2e64dfac4625cd】
Information about me.
【1b7994ba0f794e1b49742a6367e9e0c6 Hobbyebe8950f0835d9653ff486769499854f】
Information about my hobby.c1a436a314ed609750bd7c7d319db4daThis is invalid HTML39528cedfa926ea0c01e69ef5b2ea9b0
PS: Here are two very convenient regular expression tools for your reference:
JavaScriptRegular expression online testing tool:
http://tools.jb51.net/regex/javascript
Regular expression Expression online generation tool:
http://tools.jb51.net/regex/create_reg
The above is the detailed content of Detailed explanation of backreference of regular expressions_regular expressions. For more information, please follow other related articles on the PHP Chinese website!