Javascript Regular Expression and String RegExp and String (1), regular expression regexp

Source: Internet
Author: User

Javascript Regular Expression and String RegExp and String (1), regular expression regexp

Preface

Regular Expressions are very important and commonly used functions of javascript. They are frequently used in large frameworks such as jquery. Recently, I took the time to learn about the relevant knowledge and record it and share it with my friends.

Mind Map

RegExp (Regular Expression) Creation Method

You can create a RegExp in either of the following ways:

Through /.... /To create a regular expression (Note :/...... /There are no single or double quotation marks on both sides)
Create a regular expression using RegExp Constructor
To better describe the pattern, the regular expression provides three identifiers: g/I/m

G: Global match: match the entire string instead of stopping the string after the first match.
I: case-insensitive matching
M: Apply the special characters (^ and $) at the beginning and end of the line to each row of a multi-line string)
For more information, see the following code:

Var regx = new RegExp ('are', 'G'); var regx1 =/are/g; // common creation methods

Main attributes of the RegExp instance

According to the RegExp constructor, we can probably guess the main attributes of RegExp. Just take a look at the instance attributes. However, note that these instance attributes cannot be retrieved through the for in traversal.

See the following code to learn more:

Var regx1 =/are/g; // common creation method console. log ("source:" + regx. source + "global:" + regx. global + "ignoreCase:" + regx. ignoreCase + "multiline:" + regx. multiline); // source: are global: true ignoreCase: false multiline: falsefor (var p in regx) {// does not enter this for loop if (regx. hasOwnProperty (p) {console. log (regx [p]) ;}}

Main method of RegExp instance-test

This method is very simple and has only one parameter. It is often used to verify whether the input parameter matches the regular expression pattern. If the expression matches, true is returned. Otherwise, false is returned. You can refer to the following code to learn more:

var regx1 = /are/g;var res = regx.test('you are a good boy!');console.log(res) ; //truevar res1 = regx.test('I am a good boy!'); console.log(res1) ; //false

RegExp instance main method-exec

This method is a very common method and needs to be well understood. It only receives one parameter, that is, the string to be matched. The returned value is an array of arr. The array stores information about the first matching item, including:

Input: string to be matched, the input value of the exec Method

Index: Position of the matching in the string

Arr [0]: A pattern-matched string

Arr [1]… Arr [n]: Specifies the n capture group string.

When using this method, note that if the global flag is not specified in the regular expression, the first match is always returned for each execution. If the global flag is set, exec is called each time, the new match will be searched in the string.

See the following code to learn more:

Var regx =/fn :( \ w +) \ s + ln :( \ w +) \ s/g; var s = "your fn: xiaoxin ln: tang right? "; Var result = regx.exe c (s); console. log (result. input); // your fn: xiaoxin ln: tang right? Console. log (result. index); // 5 console. log (result [0]); // fn: xiaoxin ln: tang console. log (result [1]); // xiaoxin console. log (result [2]); // tang console. log (result [3]); // undefined is printed because there are only two capture groups.

RegExp constructor attributes

For more information about Function Attributes, see static attributes of classes in other programming languages (such as java). These attributes are shared by all RegExp instances, that is, all RegExp can access and modify these attributes. When an instance executes the test or exec method, the values of these attributes will also change.

We can remember these attributes according to our own understanding:

Input: the input parameter of the test or exec method. Parameter alias: $-
LastMatch: the last match. Parameter alias: $ &
LeftContext: the string on the left of the matching item. Parameter alias: $'
RightContext: the string to the right of the matching item. Parameter alias: $'
1, 2, $3 .... : Capture the string corresponding to the group.

Of course, these values can be calculated by executing the results returned by exec on the RegExp instance. Why do we need to set these attributes in the RegExp constructor? *

See the following code to learn more:

var regx = /fn:(\w+)\s+ln:(\w+)\s/g;var s ="your fn:xiaoxin ln:tang right?";var result = regx.exec(s);console.log(RegExp.input); //your fn:xiaoxin ln:tang right?console.log(RegExp.lastMatch); //fn:xiaoxin ln:tangconsole.log(RegExp.leftContext); //yourconsole.log(RegExp.rightContext); //right?console.log(RegExp.$1); //xiaoxinconsole.log(RegExp.$2); //tang

RegExp-metacharacters

Similar to regular expressions in other languages, js regular expressions also contain metacharacters that have special purposes and meanings. Therefore, escape these characters during use, escape by adding '\' before these characters. JS regular expressions are metacharacters:

([{\ ^ $ | )? * +.]}

RegExp-Greedy match and lazy match

Greedy matching means that in the matching process of a regular expression, the larger the matching length, the better. In the JS regular expression, the lazy qualifier is '? ', Add '? 'Requires a lazy match. For details, refer to the following code:

Var s = 'I am a good boy, you are also a good boy! '; Var regx =/good. * boy/g; // greedy match with console.log(regx.exe c (s) [0]); // good boy, you are also a good boyvar regx1 =/good. *? Boy/g; // lazy matching le.log(regx1.exe c (s) [0]); // good boy

The above content is a small Editor to share with you the javascript Regular Expression and String RegExp and String (1), the next article to share with you the javascript Regular Expression and String RegExp and String (2) Hope you like it.

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.