In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
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 explains how js regularities match the literal amount of strings. The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn how js regularities match the literal amount of strings.
What is JS JS is the abbreviation of JavaScript, it is a literal scripting language, its interpreter is called JavaScript engine, is a part of the browser, mainly used for web development, can add a variety of dynamic effects to the website, make the web page more beautiful.
Yesterday, I talked about / "(?:\. | [^"]) * "in" matching string literals of js regular Learning Notes "/ is a good expression, because it meets our requirements, so this expression is available, but not necessarily the best.
In terms of performance, he is very bad, why do you say that, because the traditional NFA engine encounters branches that match from left to right?
So it will use\. Match every character and use [^ "] to match when you find something wrong.
For example, a string like this: "123456\ 78\" 90 "
A total of 16 characters, in addition to the first "direct match success, there are still 15, only 2 escapes (4 characters), so\. Will fail 10 times, only 2 times succeed.
For these 10 failed matches, you need to use [^ "] after backtracking to make the match successful. Of course, the last match will succeed directly.
Obviously, normal strings can not all be escaped, normal strings are the mainstream, of course, do not rule out the case of deliberate full escape.
So this rule takes 10 backtracking to match. What if the string grows to 1K 1m?
So do we have to change the rule and change the position before and after?
Is it / "(?: [^"] |\.) * "/? Oh, it doesn't seem to be right, so the escape can't be matched.
So you have to modify / "(?: [^"\] |\\.) * / so that you will OK. If you encounter\ escape, you will use\. To try to match.
However, there is still a problem because we cannot match multiple lines of characters because we have filtered out\ nin [^ "\\].
String\ wrapping is allowed in js, but the modified rule will not match such a string, so we have to continue to fix it.
Because. There is no way to match line breaks, so we have to express them in other ways.
. Is used to match all characters except newline characters, do we have to [.\ n] to represent it?
This is wrong because of the [] character set. No longer represents all characters except newline characters, but characters. It's just a character of himself.
What are we going to do?
Actually, change your mind.
\ d means 0-9
\ d means [^ 0-9]
Then [\ d\ D] means everything, doesn't it? New friends don't know if they can digest this knowledge point. )
The same goes for [\ s\ S] [\ w\ W].
So / "(?: [^"\] |\ [\ d\ D]) * "/ this will meet our requirements.
It worked well.
Come back and analyze his current performance.
The same string: "123456\'78\" 90 ", regular /" (?: [^ "\] |\ [\ d\ D]) *" /
A total of 16 characters, in addition to the first "direct match success, there are 15, there are 2 escapes (4 characters), [^"\] can match 10 characters, only 2 failures.
Why not four failures? there are obviously four characters. Although it is 2 characters, the match fails when reading the first\, and then matches successfully with\ [\ d\ D].
It takes up two characters\\ the next time you start to match with the next o, so there are only 2 backtracking.
Only 2 backtracking is required, and then match successfully with\ [\ d\ D]. Of course, the last "will be a direct match."
So the number of backtracking has been reduced from 10 times to 2 times. Although regular is much bloated than yesterday, at least the performance has been improved by more than one level.
Thank you for reading, the above is the content of "js regular how to match the literal amount of string". After the study of this article, I believe you have a deeper understanding of how js regular matches the literal amount of string, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.