Home > Web Front-end > JS Tutorial > JavaScript Enhancement Tutorial—Regular Expression Backtracking

JavaScript Enhancement Tutorial—Regular Expression Backtracking

巴扎黑
Release: 2016-11-25 15:20:52
Original
1090 people have browsed it

This article is the official HTML5 training tutorial of H5EDU organization. It mainly introduces: JavaScript enhancement tutorial - Regular expression backtracking

The following example demonstrates the process of processing regular expressions:

/h(ello|appy) hippo/. test("hello there, happy hippo");
This regular expression is "hello hippo" or "happy hippo". When the matching process starts, it will first search for an h. The first letter of the target string happens to be h, so it is immediately turn up. Next, the subexpression (ello|appy) provides two processing options. The regular expression selects the leftmost option (the selection is always made from left to right), and checks whether ello matches the next one in the string. character, the match is successful, and the regular expression matches the subsequent spaces. Since the h in hippo cannot match the t in the next string, the match cannot continue. At this point, the regular expression cannot give up because it has not exhausted all the options. Then it will backtrack to the nearest decision point (matching the position after the first character h) and try to match the second branch. The match was not successful, and there are no more options, so the regular expression believes that matching from the first character of the string cannot be successful, so it tries again from the second character. He did not find h, so he continued searching until the h in "happy" was matched at the 14th string position, and then entered the branch process again. This time he failed to match ello, but he backtracked and tried the second branch process. Finally, the entire string "happy hippo" was matched.
Matching successful.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template