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

An example Analysis of regular expression backtracking in js

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the js regular expression backtracking example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

When a regular expression scans the target string, the components of the regular expression are scanned one by one from left to right, testing whether a match can be found at each location. For each quantifier and branch, you must determine how to proceed. If it is a quantifier (such as *, +? Or {2,}), the regular expression must determine when to try to match more characters; if a branch is encountered (through the | operator), the regular expression must choose one of these options to try.

When the regular expression makes such a decision, it remembers another option if necessary, in case it is returned. If the selected scheme matches successfully, the regular expression continues to scan the regular expression template, and if the rest of the match is also successful, the match is over. However, if the selected scenario fails to find a match, or if a subsequent match fails, the regular expression goes back to the last decision point and selects one of the remaining options. Continue until a match is found, or all possible combinations of quantifiers and branching options fail, then move to the next character at the beginning of the process and repeat the process.

For example, the following code demonstrates how this process handles branches through backtracking.

/ h (ello | appy) hippo/.test ("hello there, happy hippo")

The above line of regular expression is used to match "hello hippo" or "happy hippo". At the beginning of the test, you need to find an h, and the first letter of the target string happens to be h, which is found immediately. Next, the subexpression (ello | appy) provides two processing options. The regular expression selects the leftmost option (branch selection is always made from left to right), checks that ello matches the next character of the string, it does, and then the regular expression matches the following spaces.

However, in the next match, the regular expression "comes to a dead end" because the h in hippo does not match the next letter t in the string. At this point, the regular expression cannot be abandoned because it has not yet tried all the options, and then it goes back to the last checkpoint (where it matches the first letter h) and tries to match the second branch option. However, because the match did not succeed, and there were no more options, the regular expression thought that the match could not be successful starting with the first character of the string, so it re-searched from the second character. The regular expression does not find h, and continues to look back until the 14th letter is found, which matches the h of happy. The regular expression then enters the branching process again, and this time the ello fails to match, but in the second branch after backtracking, it matches the entire string "happy hippo", and the match is successful.

For another example, the following code demonstrates backtracking with repetitive quantifiers.

Var str = "

Para 1.

"+"

"+"

Para 2.

"+" Div. ";

. * / i.test (str)

The regular expression first matches the three letters at the beginning of the string

And then. *. The dot means to match any character except the newline character, and the asterisk, the "greedy" quantifier, means to repeat zero or more times, matching as many times as possible. Because there is no newline character in the target string, the regular expression will match all the remaining strings! However, because there is more to match in the regular expression template, the regular expression tries to match

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