JavaScript Regular Expressions

Source: Internet
Author: User

Regular type

Regular expressions, also known as regular expressions. (English: Regular Expression, often abbreviated in code as regex, RegExp, or re), a concept of computer science. Regular tables are often used to retrieve and replace text that conforms to a pattern (rule).

1. Define the regular type

var patt1=new RegExp("eee");var part2=/eee/
The method of the regular formula

1. Test ()
Reg2.test ("You string"), retrieves whether the passed-in string matches the regular, and returns a value of TRUE or False

2. EXEC ()
Performs a regular match, returns a result of object, does not match or NULL, and only performs one match. If you configure the "G" global match, you need to call a match one result at a time

var patt1=new RegExp("e","g");var result = patt1.exec("The best things")console.log(result); result = patt1.exec("The best things")console.log(result); result = patt1.exec("The best things")console.log(result); // [ ‘e‘, index: 2, input: ‘The best things‘ ]// [ ‘e‘, index: 5, input: ‘The best things‘ ]// null

Form validation
This is the match for the value of the form element.

String substitution

   //实现replaceAll的效果    var str2 = "this is test str";    var str3 = str2.replace(/t/g,‘xxx‘);    //xxxhis is xxxesxxx sxxxr

Grammar

New RegExp (patternattributes);

Parameters

Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.

The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

return value

A new RegExp object that has the specified pattern and flags. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.

Without the new operator, and REGEXP () as a function call, it behaves as if it were called with the new operator, except that when pattern is a regular expression, it only returns pattern, and no longer creates a new The RegExp object.

Thrown

SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.

Modifier
modifier Description
I Performs a match that is not case sensitive.
G Performs a global match (finds all matches rather than stops after the first match is found).
M Performs multi-line matching.
Square brackets

Square brackets are used to find characters in a range:

An expression Description
[ABC] Look for any characters between square brackets.
[^ABC] Look for any characters that are not in square brackets.
[0-9] Look for any number from 0 to 9.
[A-z] Finds any characters from a to lowercase z from a small write.
[A-z] Look for any characters from uppercase A to uppercase Z.
[A-z] Look for any characters from uppercase A to lowercase z.
[ADGK] Finds any character within a given collection.
[^ADGK] Finds any character outside the given collection.
(Red|blue|green) Finds any of the specified options.
Metacharacters

Metacharacters (metacharacter) is a character that has a special meaning:

Meta character Description
. Finds a single character, in addition to line breaks and line terminators.
\w Find the word character.
\w Finds non-word characters.
\d Find numbers.
\d Finds non-numeric characters.
\s Find white space characters.
\s Finds non-whitespace characters.
\b Matches the word boundary.
\b Matches a non-word boundary.
/ Find NUL characters.
\ n Find line breaks.
\f Find a page break.
\ r Look for the carriage return character.
\ t Find tabs.
\v Find vertical tabs.
\xxx Find the characters that are specified in octal number XXX.
\xdd Finds the characters specified in hexadecimal number DD.
\uxxxx Finds the Unicode characters specified in hexadecimal number xxxx.
Quantifiers
quantifier Description
n+ Matches any string that contains at least one n.
N Matches any string that contains 0 or more N.
N? Matches any string that contains 0 or one n.
N{X} Matches a string that contains a sequence of X N.
N{x,y} Matches a string containing a sequence of X to Y N.
N{x,} Matches a string that contains at least X N of a sequence.
n$ Matches any string that ends with N.
^n Matches any string that begins with N.
? =n Matches any string immediately followed by the specified string n.
?! N Matches any string that does not follow the specified string n immediately thereafter.
RegExp Object Properties
Properties Description FF IE
Global RegExp whether the object has a flag g. 1 4
IgnoreCase RegExp whether the object has a flag I. 1 4
LastIndex An integer that indicates the character position at which to start the next match. 1 4
Multiline RegExp whether the object has a flag m. 1 4
Source The source text of the regular expression. 1 4
RegExp Object Methods
Method Description FF IE
Compile Compiles the regular expression. 1 4
Exec Retrieves the value specified in the string. Returns the found value and determines its location. 1 4
Test Retrieves the value specified in the string. Returns TRUE or FALSE. 1 4
Methods for String objects that support regular expressions
Method Description FF IE
Search Retrieves a value that matches a regular expression. 1 4
Match Finds a match for one or more regular expressions. 1 4
Replace Replace the substring that matches the regular expression. 1 4
Split Splits a string into an array of strings. 1 4
 

JavaScript Regular Expressions

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.