JavaScript Regular Expressions

Source: Internet
Author: User
Tags alphabetic character assert control characters

1. These two days read the web's JavaScript regular expression to make notes hurriedly paste up to record

Introduce a graphical regular legend, which means it's very powerful. Official website Http://regexper.com/githup https://github.com/javallone/regexper-static

Follow the installation on the Readme to start

The service starts up on the 127.0.0.1:8080, you can try to put up the regular that you didn't understand before.

Attach JS regular related knowledge to explain the notes


Word Matching
\bis\b
Words are not characters on either side

Link match
Http:\/\/.+\.jpg
http://starts with a. jpg ending character

http: (\/\/.+\.jpg) grouping, substituting $ \$1 for grouped content

Date substitution
\D{4}[/-]\D{2}[/-]\D{2} Four digits beginning with a link-or/square bracket or meaning, followed by two digits
^\d{4}[/-]\d{2}[/-]\d{2}$ ^ angle brackets denote the beginning $ means end
^ (\d{4}) [/-] (\d{2}) [/-] (\d{2}) $ group $1,$2,$3, respectively, for first group, second group, third group

RegExp Object
JavaScript supports regular expressions through built-in object regexp
There are two ways to instantiate a RegExp object
. literal
ver reg =/\bis\b/g
' He's a boy. This is a dog. Where is she? '. Replace (Reg, ' is ');
/g to match the full text
No add/g
"He's a boy." This is a dog. Where is she? "matches only the first one
Add/g
"He's a boy." This is a dog. Where is she? "

. constructors
var reg = new RegExp (' \\bis\\b ', ' G ');
' He's a boy. This is a dog. Where is she? '. Replace (Reg, ' is ');

Modifier
G:global Full Text Search, do not add, search to first stop
I:ignorecase ignoring case, default case sensitivity
M:multiple lines multi-line search
Metacharacters
-Literal text characters
-Meta characters

Metacharacters is a non-alphabetic character that has a special meaning in a regular expression
. * + ? $ ^ .| \ () {} []
Metacharacters
\ t Horizontal tab
\v Vertical Tab
\ n line break
\ r return character
Space-out character
\f Page Break
\CX control characters corresponding to x Ctrl + x
Character class
In general, a character of a regular expression corresponds to a string of one character
Expression ab\t meaning ab plus horizontal tab
Match a certain type of character
We can use metacharacters [] to build a simple class
A class refers to an object that conforms to certain attributes, a generic rather than a character.
expression [ABC] the character a B C is classified as a class, the expression can match such characters
' A1b2c3d4 '. Replace (/[abc]/g, ' x ')
"X1x2x3d4"
The character class takes the inverse
Creating a reverse class/negative class using the meta-character ^
A reverse class means something that is not part of a class.
expression [^ABC] means content that is not character a B C
Scope class
A regular expression provides a scope class
So we can use [A-z] to connect two characters representing any character from A to Z
This is a closed interval, which includes a and z itself.

Example ' A1b2d3x4z9 '. Replace (/[a-z]/g, ' Q ') results "q1q2q3q4q9"

Inside the [] class, it is possible to ligatures the [a-za-z] representation of uppercase and lowercase letters

In the range, if you want to match--add one to the back.
' 2016-09-12 '. Replace (/[0-9-]/g, ' A ');
Pre-defined Classes
Regular expressions provide pre-defined classes
. [^\r\n] In addition to carriage return all characters except line break
\D[0-9] Numeric characters
\D[^0-9] Non-numeric characters
\s[\t\n\x0b\f\r] whitespace characters
\s[^\t\n\x0b\f\r] non-whitespace characters
\W[A-ZA-Z0-9] Word character (letter, numeric underline)
\W[^A-ZA-Z0-9] non-word characters
Example: match ab+ digit + any word regular means ab[0-9][^\n\t] ab\d is represented by a predefined class.
The bounding regular expression also provides several commonly used bounds-matching characters
^ Starting with XX
$ to end with XX
\b Word boundaries
\b Non-word boundaries
Example: ' This was a boy '. Replace (/is/g, ' 0 '); match result "Th0 0 a Boy"
Add word boundary ' This is a boy '. Replace (/\bis\b/g, ' 0 '); match result "This 0 a boy"
Add non-word boundary ' This is a boy '. Replace (/\bis\b/g, ' 0 '); Match result "Th0 is a boy"

' @[email protected]@ '. Replace (/@./g, ' Q '); match result: "[email protected]"
' @[email protected]@ '. Replace (/^@./g, ' Q '); match result: "[Email protected]@"
' @[email protected]@ '. Replace (/[email protected]$/g, ' Q '); Match result: "@[email protected]"


/M example
var mulstr = ' @123\[email protected]\[email protected]\n '
Mulstr.replace (/^@\d/g, ' X ') ;
Result
"X23
@456
@789
"
Mulstr.replace (/^@\d/gm, ' X ');
Result
"X23
X56
X89
"
quantifier we want to match a string that appears consecutively 20 times \d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d
? Occurs 0 times or once (up to one time)
+ occurs one or more times (at least once)
* occurs 0 or more times (any time)
{n} appears n times
{n,m} appears n times to M times
{n} at least n times this useful
up to 10 times/d{ 0,10}
Greedy mode
\d{3-6}123456789 all satisfy the regular, the regular will always match to know the match fails, this is greedy mode.
' 12345678 '. Replace (/\d{3,6}/g, ' X '); match result "x78"
Non-greedy mode
Let regular expression match as little as possible, that is, once the match succeeds the match no longer continues the attempt is greedy mode
Practice: After the quantifier is added?
' 12345678 '. Replace (/\d{3,6}?/g, ' X '); match result "xx78"
Group: Match string Byron 3 consecutive scenes byron{3} The quantifier is the word that works on the nearest.
Use () to achieve grouping functions, making two actions on grouping
(Byon) {3}

or |
byron| Casper Word appears two any one
Example ' Byroncasper '. Replace (/byron|casper/g, ' X '); Match result: "XX"
Reverse reference 2015-12-25 ==>12/25/2015
Group capture
' 2015-12-25 '. Repalce (/(\d{4})-(\d{2})-(\d{2})/g, ' $2/$3/$1 ') represents the first grouping
Group Ignore
Do not want to capture some of the groupings, only need to add within the group?: it's all right.
(?: Byron). (OK) indicates that the first group is ignored, and the match is OK
Prospect
Regular expressions are parsed from the end of the text to the end of the text, which is called the "front"
The forward-looking is that when a regular expression is matched to a rule, the forward check is consistent with the assertion, looking back and forward-looking opposite
JavaScript does not support looking back
Compliant and non-conforming specific assertions called positive/positive matching and negative/negative matching

Forward Looking exp (? =assert)
Negative forward exp (?! Assert

\w (? =\d) ' A2*3 '. Replace (/w (? =\d)/g, ' X '); Not only matches the word character, it's followed by a number.
Object Properties
. Global Full text
. Ignore Case Capitalization
. Multiline Multi-line
. LastIndex the next position of the last character of the current expression matching content
The text string of the. SOURCE Regular expression
object property settings cannot be modified after
Test and Exec methods
RegExp.prototype.test (str)
A string used to test for the existence of a matching regular expression pattern in a string parameter
False if there is a return true
REG1 =/\w/;
REG2 =/\w/gim;


The test () method is affected by lastindex
while (Reg2.test (' ab ')) {
Console.log (Reg2.lastindex);
}
It only executes two times, so test () lastindex affects the regular expression, and without g there is no such problem.
RegExp.prototype.exec (str)
Performs a search on a string using a regular expression and updates the properties of the global RegExp object to reflect the matching result
Returns null if no matching text is returned, or returns an array of results

-index declares the position of the first character of the matched text
-input to store retrieved strings string
Non-global calls
When you call the EXEC () of a non-global RegExp object, the data is returned
The first element is the text position that matches the regular expression
The second element is the text (if any) that matches the first subexpression of the Regexpobject.
The third element is the text that matches the second sub-expression of the RegExp object.
Example
var reg3 =/\d (\w) \d;
var reg4 =/\d (\w) \d/g;
var ts = ' 1a2b3c4d5e ';
var ret = reg3.exec (TS);

Console.log (reg3.lastindex + ' \ t ' + Ret.index + ' \ t ' + ret.tostring ());
Console.log (reg3.lastindex + ' \ t ' + Ret.index + ' \ t ' + ret.tostring ()); The
//non-global lastindex is not working.
//The while loop here is because there is lastIndex, and the second time after LastIndex is False
while (ret = reg.exec (ts)) {
Console.log (reg4.lastindex + ' \ t ' + Ret.index + ' \ t ' + ret.tostring ()); The
}
String.protatype.search (reg)
. Search () method is used to retrieve the substring specified in the string, or to retrieve a substring that matches the regular size of the string
method to return the first matching result index, Cannot find the return-1
The search method does not perform a global match, it ignores the G flag. Always start from scratch
String.prototype.match (reg)
. The match () method retrieves a string to find one or more regexp matching text
. RegExp has a flag g has a large effect on the result
non-global calls
. If RegExp does not have a flag G, then the match method can only perform a match in the string
. If no matching text is found, NULL
is returned. Otherwise it will return an array containing information about the matching text it finds
His results are the same as exec.
The first element is the text position that matches the regular expression
The second element is the text (if any) that matches the first subexpression of Regexpobject
The third element is the text that matches the second sub-expression of the RegExp object, the
Global call
does not find any matching substrings, or null
returns an array if one or more matching substrings are found

A matching substring is stored in the array element
String.prototype.splite ()
Splite () is passed in to the regular expression.
String.prototype.replace (' firststr ', ' replacestr ')

JavaScript Regular Expressions

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.