JS Regular expression (reprint)

Source: Internet
Author: User

In JavaScript, the RegExp object represents a regular expression used to match a string.

One or two methods of definition:

1. Direct Volume Method:

/pattern/attribute

2. Object method:

New RegExp (Pattern,attribute);

Parameter description:

Pattern is a string that specifies the pattern of a regular expression or other regular expression;

attribute is an optional string that is used for pattern matching ranges, including i,g,m three characters, representing case-sensitive matching, global matching, and multiline matching, respectively. (if pattern is a regular expression and not a string, you must delete the parameter)

i-->ignorecase;

g-->gloable;

m-->multiline;

Second, the regular expression syntax description:

The syntax of a regular expression is described in seven ways, such as modifiers, brackets, metacharacters, quantifiers, RegExp object Properties, RegExp object methods, and methods that support a regular expression string object:

1. Modifier:

Modifier Describe
I Perform a match that is not case sensitive
G Perform a full match (find all occurrences, not end after the first match is found)
M Perform multi-line matching

2. Square brackets:

An expression Describe
[ABCD] Finds any character within a given set
[^ABCD] Find any character that is not in a given collection
[0-9] Find any number from 0 to 9
[A-z] Find any lowercase letters from a to Z
[A-z] Find any uppercase letters from A to Z
[A-z] Find any letters from uppercase A to lowercase Z
[Red|blue|green] Find any of the specified options

3, meta-character

if not
metacharacters description
. find any character, not including line break and line Terminator
\w Find word characters, word characters include a-Z, A-Z, 0-9, and underline
\w find non-word characters
\d Find numbers
\d Find non-numeric characters
\s to find white space characters, including spaces, tabs, carriage returns, line feeds, vertical line breaks, and page breaks
\s find non-whitespace characters
\b matches word boundaries
\b matches a non-word boundary
+ find null characters
\ n finds a newline character, returns the location where the newline character was found, and returns 1
\f Find a page break
\ r Find carriage return
\ t find tabs
\v Find vertical Tabs
\xxx find characters with octal number xxx
\xdd to find characters specified in hexadecimal DD
\uxxxx to find Unicode characters in hexadecimal xxxx

4. quantifier

Quantifiers Describe
n+ Match a string that contains at least one n
N Match a string containing 0 or more n
N? Match a string containing 0 or 1 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 ending with n
^n Matches any string that begins with n
!=0 Matches any string immediately followed by the specified string n
?! N Matches any string that is not immediately followed by the specified string n

5. RegExp Object Properties

Property Describe
Gloable RegExp whether the object has a G flag, Regexpobject.global, returns a Boolean type
IgnoreCase Whether the RegExp object has an I flag, Regexpobject.global, returns a Boolean type
Multiline RegExp whether the object has an M flag, Regexpobject.global, returns a Boolean type
LastIndex An integer that represents the next location to start matching
Source Regular expression original text, returning the text used for pattern matching

6. RegExp Object method

Method Describe
Compile Compiles the regular expression. Usage: regexpobject.compile (regexp,modifier) modifier Specifies the type of matching-->i,m,g
Exec Retrieves the value specified in the string, returns the found value and determines its location, or returns null if not found
Test Retrieves the value specified in the string, returning a Boolean type. Used to determine whether a string matches a pattern, regexpobject.test (string)

7. Methods of String objects that support regular expressions

Method Describe
Search Retrieves a value that matches a regular expression or a specified substring. Usage: Stringobject.search (regexp)
Match Matches a specified value or a string that matches one or more regular expressions. Usage: Stringobject.match (searchvalue); Stringobject.match (RegExp)
Replace Replace other characters with some characters or replace substrings that match a regular expression. Usage: stringobject.replace (regexp/substr,replacement), the former is a substring or a regular expression to be replaced, the latter is the replacement text
Split Used to split a string into an array of strings. Usage: stringobject.split (separator,howmany). The former must be a string or regular expression, starting at the location specified by the parameter.

JS Regular expression (reprint)

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.