JS use Regular (simple introduction)

Source: Internet
Author: User


RegExp Object

The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.
Direct volume syntax

/pattern/attributes

Syntax for creating REGEXP objects:

New REGEXP (pattern, attributes);

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 returns only pattern and no longer creates a new 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 perform a match on case insensitive.
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:
Expression description
[ABC] finds any character between square brackets.
[^ABC] finds any characters that are not in brackets.
[0-9] Find any number from 0 to 9.
[A-z] finds any characters from lowercase a to lower Z.
[A-z] finds any characters from uppercase A to uppercase Z.
[A-z] finds any characters from uppercase A to lowercase z.
[ADGK] finds any character within a given collection.
[^ADGK] finds any character outside the given set.
(Red|blue|green) to find any of the specified options.
Metacharacters

Metacharacters (metacharacter) is a character that has a special meaning:
Metacharacters description
. Finds a single character, in addition to line breaks and line terminators.
\w find word characters.
\w Find non-word characters.
\d find numbers.
\d to find non-numeric characters.
\s Find white space characters.
\s find non-whitespace characters.
\b matches the word boundary.
\b Matches a non-word boundary.
To find the NUL character.
\ nto find newline characters.
\f Find a page break.
\ r to find the carriage return character.
\ t to find a tab.
\v Find vertical tabs.
\xxx finds characters that are specified in octal number XXX.
\XDD finds the characters specified in hexadecimal number DD.
\uxxxx finds Unicode characters that are 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 containing a sequence of X N.
N{x,y} matches a string containing a sequence of X or 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 following the specified string n.
?! n matches any string that does not follow the specified string n immediately thereafter.
RegExp Object Properties
Property Description
Whether the global RegExp object has a flag g.
IgnoreCase whether the RegExp object has a flag I.
LastIndex An integer that indicates the position of the character at which to begin the next match.
Multiline whether the RegExp object has a flag m.
The source text of a regular expression for a source.
RegExp Object Methods
Method description
Compile compile the regular expression.
EXEC retrieves the value specified in the string. Returns the found value and determines its location.
Test retrieves the value specified in the string. Returns TRUE or FALSE.
Methods for String objects that support regular expressions
Method describes FF IE
Search retrieves the value that matches the regular expression.
Match finds the matching of one or more regular expressions.
Replace replaces the substring that matches the regular expression.

Split splits the string into an array of strings.


A simple example: the user name and mailbox detection, the use of regular.

<! DOCTYPE html>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JS use Regular (simple introduction)

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.