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

The method of how JavaScript regular expression matches the literal quantity of string

2025-05-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "JavaScript regular expression how to match the literal amount of string method", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "JavaScript regular expression how to match the literal amount of string method"!

The first time I encountered this problem was when I wrote the code highlight about two years ago. There are three generations from the solution at that time to now, Gaga. I think it's getting better and better.

The first generation:

/ / at that time, I was not very proficient in regular, nor did I (?:.) This habit is written by looking for closing quotation marks as the entry point. The train of thought is confused, and there are also errors. / / for example, if the literal quantity "abc\" will match to "abc\", the correct result should be "abc\" .var re = / ('('|. *? ([^\\]'|\)) | "(" |. *? ([^\] "|\")) / g)

The second generation:

/ / this match is basically the same as that of the first generation. It also looks for closing quotation marks and solves the first generation of bug.var re = / (['"]) (?:. *? [^\] (?:\) *) by adding * to\\.

The third generation:

/ / to be honest, the third generation suddenly came up with the question last night (later revised). It supports the literal amount of multi-line strings, and there has been a great change in thinking. Var re = / (['"]) (?:\ (:\ r\ n | [\ s\ S]) | [^\\ r\ n]) *?\ 1g

Here is a very common technique in the rule, let's call it "preemption". In a rule such as / a |. /, the more you get ahead, the more you match first. by "preempting" some characters, you can avoid a lot of trouble. It's a bit abstract, in the above example, /\ [\ s\ S] / can naturally grab escape characters like\ ", so you don't have to worry about the" in the escape characters affecting the match. This is local preemption, and there is a wider range, such as the string in the comment or the comment in the string, as long as all the relevant matches are written in the same rule, it must be the first to encounter. For example, / (comment regular) | (string regular) / g, you can match the content in this way, and then make a further judgment.

It may not be of much use to most people, but what if? Best wishes~

Thank you for reading, the above is the content of "JavaScript regular expression how to match the literal amount of string". After the study of this article, I believe you have a deeper understanding of the problem of how JavaScript regular expression 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report