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

Explanation on the usage of matching string literals of js regularization

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "js regular matching string literal usage explanation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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.

Today, I read a few examples in Chapter 5. I gained a little bit, and recorded it as a review and shared it as well.

There are many types of string matching problems. Today we will discuss string matching in js code. (because I want to write a grammar highlight after learning, so I use js code as an example.)

Var str1 = "I am string 1 Oh, take me away", str2 = "I am string 2 Oh, take me away"

For example, a string like this is easy to match with / "[^"] * "/ g.

PS: the white screenshot is the result of running in the chrome 34 console, and the dark gray is the sublime text result.

It's easy to get the content, but dear, have you ever thought that escaping is allowed in js strings.

Var str1 = "I am string 1 Oh,\" take me away\ ", str2 =" I am string 2 Oh,\ "take me away\"

This is not the result we want, so we have to deal with the problem of escape.

The rule of escape is\ followed by a character, so we use\. To match it, there is a regular / "(?:\. | [^"]) * "/ g

May not be familiar with the regular friends, suddenly changed from / "[^"] * "/ g to /" (?:\\. | [^ "]) *" / g can not be understood, I will briefly explain.

(?:) is a non-capture group, but the content will not be grabbed and saved in memory.

\. Used to match escape characters such as\ "\'\ a, which consumes 2 characters if the match is successful.

[^ "] matches all characters except", and consumes 1 character if the match is successful.

So what this expression means is, match\ first. This escape consumes the correct escape if it succeeds, and matches with [^ "] if it is not successful.

Consuming the correct escape means that, for example, "aa\\ aa\" aa\ ufffaa "will be matched properly, because these are normal escapes.

In the form of "aa\ aa\" aa\ ufff\\ "aa" here\\ "where\\ will be matched, and" is not satisfied. Nor satisfied with [^ "]

So the matching result will be that the aa after "aa\\ aa\" aa\ ufff\\ "cannot be matched.

So now we have a strong regular that can effectively match strings in js code.

Sao and so on, it seems that there is something wrong.

The js string allows you to wrap lines like this.

Var str = "Hello, everyone. I'm js."

But can such a string match with the previous rule? The answer is yes.

Because it can't be. Match, but can be matched by [^ "], and the last character\ r,\ n or\ r\ n (depending on the system) can be matched by [^"], so we inadvertently wrote a strong expression.

Now there is one last question left, which is the match of O'Neill.

Let's modify the expression / "(?:\\. | [^"]) * "|'(?:\\. | [^']) *'/ g.

Let's test it:

Var str1 = "I am string 1 Oh,\" take me away\ ", str2 ='I am string 2,\ 'take me away\'

The match is successful.

This is the end of the introduction to the usage of "js regular matching string literals". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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