JavaScript Intensive Tutorial--Regular expression backtracking

Source: Internet
Author: User

This article is the official HTML5 training course for h5edu institutions, mainly introduces: JavaScript intensive Tutorial--Regular expression backtracking

The following example shows the process of processing a regular expression:

/h (ello|appy) hippo/.test ("Hello there,happy hippo");
This regular expression "Hello Hippo" or "Happy Hippo". At the beginning of the matching process, an H is first found, and the first letter of the target string is exactly H, and is immediately located. Next, the subexpression (ello|appy) provides two processing options, the regular expression selects the leftmost option (the selection always goes from left to right), checks whether the Ello matches the next character in the string, matches successfully, and the regular expression matches the subsequent spaces. Because the h in Hippo cannot match T in the next string, the match cannot continue. At this point, the regular expression cannot be discarded, because it is not yet complete with all the options in the city, and then he goes back to the nearest decision point (matching the position behind the first character H) and tries to match the second branch. The match does not succeed, and there are no more options, so the regular expression believes that the first character of the string match is unsuccessful, so start again with the second character. He did not find H, so he continued the search to know that the position of the 14th string was matched to H in "happy" and then again into the branching process, which failed to match Ello this time, but after backtracking and attempting the second branching procedure, the entire string "Happy Hippo" was matched.
The match was successful.

Click to enter JavaScript Intensive tutorial: http://www.h5edu.cn/htm/step/h5edu_44.html

JavaScript Intensive Tutorial--Regular expression backtracking

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.