JavaScript Regular expression Packet matching detailed _ basics

Source: Internet
Author: User
Tags first string

Group

The following regular expressions can match kidkidkid:

/kidkidkid/

And another more elegant way to do this is:

/(Kid) {3}/

Here a small whole of the parentheses parcel is called grouping.

Candidate

In a group, you can have multiple candidate expressions, separated by |:

var reg =/I love (him|her|it)/;

Reg.test (' I love him ')  //True 
reg.test (' I love her ')  //True
reg.test (' I love it ')  //True
reg.test (' I love them ')//False

This is the equivalent of "or".

Capturing and referencing

The string that is matched (captured) by the regular expression is temporarily saved. The string captured by the group is numbered from 1, so we can refer to these strings:

var reg =/(\d{4})-(\d{2})-(\d{2})/
var date = ' 2010-04-12 '
reg.test (date)

regexp.$1//
regexp.$2//
regexp.$3//12

The first captured string is referenced, and the second is $, and so on.

Match with replace

The captured string can be referenced directly in the String.prototype.replace method's pass parameter. For example, we want to change the date 12.21/2012 to 2012-12-21:

var reg =/(\d{2}). (\d{2}) \/(\d{4})/
var date = ' 12.21/2012 '

date = Date.replace (Reg, ' $3-$1-$2 ')//date = 2012-12-21

By the way, it is sometimes possible to gracefully solve some of the problems by passing the iteration function to replace.

Converting banned words to an asterisk with a number of words is a common feature. For example, the text is the kid is a Doubi, where kid and Doubi are prohibited words, then the conversion should be * * * * * * * * *. We can write this:

var reg =/(Kid|doubi)/g
var str = ' Kid ' a doubi '

str = str.replace (reg, function (word) {return
  word.replace (/./g, ' * ')
})

Capture of nested groupings

If you encounter a nested grouping of similar/((Kid) is (Doubi))/, what is the order of capture? To try:

var reg =/((Kid) is (A (Doubi))/
var str = ' Kid is a Doubi '

reg.test (str)//True

regexp.$1//Kid Be a Do Ubi
regexp.$2//Kid
regexp.$3//a Doubi
regexp.$4//Doubi

Rules are captured in the order in which they appear in the left parenthesis.

Reverse reference

A reference can also be made in a regular expression, which is called a reverse reference:

var reg =/(\w{3}) is \1/

reg.test (' Kid are kid ')//True
reg.test (' Dik is Dik ')/True
reg.test (' Kid is di K ')//False
Reg.test (' Dik is Kid ')//False

\1 refers to the first string that is captured by grouping, in other words, the expression is dynamically determined.

Note that if the number crosses the line, it is treated as an ordinary expression:

var reg =/(\w{3}) is \6/;

Reg.test (' Kid is Kid '); False
Reg.test (' Kid is \6 ');  True

Type of grouping

There are four types of groupings:

Capture Type-()
Non-capture type-(?:)
Forward-looking type-(? =)
Reverse-looking type-(?!)
We've been talking about capture groupings, and only this grouping will hold the strings that match.

Non-capture grouping

Sometimes, we just want to split a group, without capturing the requirements, you can use a non-capture grouping, followed by a left parenthesis syntax::

var reg =/(?: \ D{4})-(\d{2})-(\d{2})/
var date = ' 2012-12-21 '
reg.test (date)

regexp.$1//
regexp.$2//21

In this example, (?: \ D{4}) grouping does not capture any strings, so the string that is captured is (\d{2}).

Forward and reverse-looking groupings

It's like you're standing there, looking forward:

Forward-looking groupings-what are you ahead of?
Negative forward-looking groupings-isn't there something in front of you?
It's a mouthful, I like to call affirmative expressions and negative expressions. First, a forward-looking example:

var reg =/kid is a (. =doubi)/

reg.test (' Kid is a Doubi ')//True
reg.test (' Kid is a Shabi ')//False

What follows Kid is a? If it is doubi to match successfully.

And the negative outlook is just the opposite:

var reg =/kid is a (?! Doubi)/

reg.test (' Kid is a Doubi ')//False
Reg.test (' Kid is a Shabi ')/True

If the forward-looking group does not capture the value. So what is the difference between it and the non-capture type? See Example:

var reg, str = "Kid is a Doubi"

reg =/(Kid are a (?:d Oubi))/
reg.test (str)
regexp.$1//Kid is a Doubi

reg =/(Kid is a (? =doubi))/
reg.test (str)
regexp.$1//Kis is a

As you can see, the strings that are matched by the non-capture groupings are still captured by the outer capture group, but not in the forward type. A forward-looking grouping comes in handy when you need to refer to the value behind and don't want to catch it together.

Finally, JS does not support back-up grouping.

Related Article

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.