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

How regular expressions match strings in js

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

Share

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

This article mainly introduces the regular expression in js how to match the string, the article is very detailed, has a certain reference value, interested friends must read it!

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 break 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 the string 1,\"

\ "take me away quickly", str2 ='I am string 2,\ "

\ 'take me away.''

The match is successful.

The above is all the content of the article "how regular expressions match strings in js". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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: 216

*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