JS Regular Expression Learning notes matching string _ Regular expression

Source: Internet
Author: User
Tags regular expression sublime text

Today I read the 5th chapter a few examples, a little harvest, recorded as a retrospective also as a share.

There are many types of matching string problems, and today we discuss string matching in JS code. (because I want to learn to write a syntax highlighting practicing, so use the JS code as an example)

Copy Code code as follows:

var str1 = "I am a string 1 Oh, quick take me Away", str2 = "I am a String 2 Oh, take me away now";

such as a string, the match is very simple/"[^"]* "/g can be.



PS: The white screenshot is the result of the operation of the Chrome 34 console, and the dark Gray is the sublime text result.

It's easy to get the content, but pro, have you ever thought that the JS string allows escape.

Copy Code code as follows:

var str1 = "I'm a string 1 Oh, \" Take Me Away \ "", str2 = "I am a string 2 Oh, \" quickly take me away \ "";

This is not the result we want, so we have to deal with the escape problem.
The escaped rule is followed by a character, so we use \. To match it, there is such a regular/"(?: \ \.| [^"]) * "/g

May be not familiar with the friend, suddenly from/"[^"]* "/g to/" (?: \ \.| [^"]) * "After/g understand, I simply explained."
(?:) is a non-capturing group and does not store content in memory.
\\. Used to match an escape character such as \ \a, which consumes 2 characters for a successful match.
[^] matches all characters except ", and a successful match consumes 1 characters."
So this expression means, first match \. This escapes if success consumes the correct escape, and if it is not successful, it is matched with [^].

Consuming the correct escape means that, for example, "Aa\\aa\" AA\UFFFAA will be properly matched because these are all in the normal escape.
In the form of "aa\\aa\" aa\ufff\\ "AA" here \ \ \ \ will be matched, and "neither meet \ \." Also not satisfied [^]
So the result of this match would be "aa\\aa\" aa\ufff\\ "after AA" could not be matched to.


So now we have a more powerful regular, you can effectively match the JS code in the string.

There seems to be something wrong.
The JS string allows the line to be folded, like this:

Copy Code code as follows:

var str = "Everyone good \
I am JS ";

But does such a string match with just the positive? The answer is yes.
Because \ can't be \. Match, but can be matched by [^] to, \ \ r, \ n, or \ r \ n (specifically what line feed is to see the system) can be matched to [^], so we inadvertently write a strong expression.

Now the last question is, ' the match.
We modify the following expression/"(?: \ \.| [^"]) *"|' (?:\ \.| [^']) * '/g just fine.

To test:

Copy Code code as follows:

var str1 = "I'm a string 1 Oh, \
\ "Take Me away quickly" ", str2 = ' I'm a string 2 Oh, \
\ ' Take me away quickly ';

Match succeeded.

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.