Matching strings for js Regular Expression learning notes

Source: Internet
Author: User

I have read several examples in Chapter 1 today, and I have learned a little bit about it. record it as a review and share it with me.

There are many types of string matching issues. Today we will discuss string matching in js Code. (Because I want to write a syntax highlight trainer after learning it, I use js Code as an example)

Copy codeThe Code is as follows:
Var str1 = "I am string 1, please take me away", str2 = "I am string 2, please take me away ";

For example, it is very easy to match such a string/"[^"] * "/g.

 



PS: White is the result of running on chrome 34 console, and dark gray is the result of sublime text.

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

 

Copy codeThe Code is as follows:
Var str1 = "I am string 1, \" Take me away \ ", str2 =" I am string 2, \ "Take me away \"";

This is not the expected result, so we need to handle the escape problem.
The escape rule is \ followed by a character, so we use \. to match it, we have such a regular /"(? : \. | [^ "]) *"/G

 
 
A friend who is not familiar with regular expressions may suddenly change from/"[^"] * "/g /"(? : \. | [^ "]) *"/G.
(? :) Non-capturing groups do not capture and save the content to the memory.
\. It is used to match escape characters such as \ "\ '\ a. If the match is successful, it will consume 2 characters.
[^ "] Matches all characters except". If the match is successful, one character is consumed.
Therefore, this expression means to first match the \. Escape. If it is successful, the correct escape will be consumed. If it is not successful, it will be matched with [^.

The correct escape means that, for example, "aa \" aa \ ufffaa "will be properly matched because these are all properly escaped.
Such as "aa \" aa \ ufff \ "aa" here \ "where \ will be matched, and" neither satisfied \\. nor does it meet [^ "]
Therefore, the matching result will be "aa \" aa \ ufff \ "followed by aa.


So now we get a strong regular expression that can effectively match strings in js Code.

Sao, etc. It seems there is something wrong.
The js string allows line breaks, as shown in the following figure:

Copy codeThe Code is as follows:
Var str = "Hello everyone \
I am a js ";

But can such a string be matched with the regular expression just now? The answer is yes.
Because \ cannot be \\. match, but it can be matched by [^ "]. \ is followed by \ r, \ n, or \ r \ n (the specific line break depends on the system) can be matched by [^ "], so we accidentally wrote a strong expression.

Now the last problem is matching.
Let's modify the expression /"(? : \. | [^ "]) *" | '(? : \. | [^ ']) *'/G.

To test:

Copy codeThe Code is as follows:
Var str1 = "I Am a string 1 ,\
\ "Take me away now \", str2 = 'I am string 2 ,\
\ 'Take me away now \'';

Matched successfully.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.