Use of regular expressions

Source: Internet
Author: User
Tags character classes

Tag: source server [] Returns the same regexp condition as CAs. Net

Regular expressions (regular expression) are an object that describes the character pattern. The RegExp class of ECMAScript
Represents a regular expression, and both String and REGEXP define strong pattern-matching and text using regular expressions
Retrieves and replaces the function.
Regular expressions are primarily used to validate the input data of the client. When the user fills out the form and clicks the button, the form will
is sent to the server, it is usually processed further by server scripts such as PHP, ASP.
Because of client authentication, you can save a lot of server-side system resources and provide a better user experience.

Optional parameters for pattern modifiers
Parameter meaning
I ignore case
G Global Match
M multi-line matching

Methods for REGEXP objects
Method functions
Test matches the pattern in the string, returning True or false
EXEC performs a matching search in a string, returning an array of results

Regular expression methods in a String object
Method meaning
Match (pattern) returns a substring in pattern or null
Replace (pattern, replacement) replaces pattern with replacement
Search (pattern) returns the start position of the pattern in a string
Split (pattern) returns an array of strings split by the specified pattern

Static properties of the RegExp object
Attribute short name meaning
Input $_ The string currently being matched
Lastmatch $& Last matching string
Lastparen $+ the matching substring in the last pair of parentheses
Leftcontext $ ' substring before last match
Multiline $* used to specify whether all expressions are used for multiple rows of Boolean values
Rightcontext $ ' substring after last match

Instance properties of the RegExp object
attribute meaning
A global Boolean value that indicates whether G is set
IgnoreCase Boolean value that indicates whether I have been set
A lastIndex integer that represents where the next match will begin with the character position
A multiline Boolean value that indicates whether M is set
Source string form of regular expression

Character classes: Single characters and numbers
metacharacters/meta symbol matching case
. Match any character except line break
[a-z0-9] matches any character in the character set in parentheses
[^a-z0-9] matches any character in a character set that is not in parentheses
\d Matching numbers
\d matches non-numeric, same as [^0-9]
\w matches letters and numbers and _
\w matches non-letters and numbers and _


Character classes: whitespace characters
metacharacters/meta symbol matching case
Match NULL character
\b Match whitespace characters
\f Matching input characters
\ nthe match line break
\ r matches carriage return character
\ t matches tabs
\s matches white space characters, spaces, tabs, and line breaks
\s matches non-whitespace characters


Character classes: Anchor characters
metacharacters/meta symbol matching case
^ Beginning of match
$ end-of-line matching
\a only at the beginning of the match string
\b Match word boundaries, words are invalid within []
\b Matching non-word boundaries
\g matches the starting position of the current search
\z matches the end of a string or a line ending
\z Match string End only


Character classes: repeating characters
metacharacters/meta symbol matching case
X? Match 0 or 1 x
x* Match 0 or any number of X
x+ matches at least one X
(XYZ) + matches at least one (XYZ)
X{m,n} matches a minimum of M, up to n x


Character classes: Alternative characters
metacharacters/meta symbol matching case
This|where|logo match this or where or any of the logos


Character classes: Record characters
metacharacters/meta symbol matching case
(string) A grouping to use for backward referencing
\1 or match the contents of the first group
\2 or $ match the contents of the second group
\3 or $ A matches the contents of the third group

Greedy lazy
+              +?
? ??
*              *?
{n} {n}?
{N,} {N,}?
{N,m} {n,m}?

Use of 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.