Home  >  Article  >  Web Front-end  >  A brief introduction to the use of regular expressions in JavaScript_Basic knowledge

A brief introduction to the use of regular expressions in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:54:481244browse

A regular expression is an object that describes a character pattern.

JavaScript's RegExp class represents regular expressions and string and regular expression definitions, using regular expressions to perform powerful pattern matching and search and replace text functionality.
Syntax:

Regular expressions can be defined using RegExp():

var pattern = new RegExp(pattern, attributes);

or simply

var pattern = /pattern/attributes;

Here is the description of the parameters:

  • Pattern: A string specifying a regular expression or other regular expression pattern.
  • attributes: Contains any "g", "i", and "m" attributes specified globally, each an optional string, case-insensitive and matching.

Brackets:

Brackets ([]) have special meaning when used in the context of regular expressions. They are used to find ranges of characters.

2015615115910343.jpg (696×355)

The range shown above is general; you can also use the range [0-3], which matches any decimal number from 0 through 3, or the range [b-v], which matches any lowercase letter, from b to v
Qualifier:

A frequency or sequence of characters within square brackets and the position of a single character can be represented by a special character. Each special character has a specific connotation. The , *, ?, and $ signs all follow a sequence of characters.
Example:

2015615115941726.jpg (677×460)

The following example will clear the concept about matching characters.

2015615115959365.jpg (686×289)

Literal characters:

2015615120019708.jpg (701×550)

Metacharacters are just preceded by a backslash, and their function is to combine alphabetic characters with special meanings.

For example, you can search for large monetary totals using the 'd' metacharacter: /([d] )000/, where d will search for any string of numeric characters.

The following is a list of metacharacters that can be used in Perl-style regular expressions.

2015615120039833.jpg (880×289)

Modifier

Several modifiers are available that can make working with regular expressions much easier, such as upper and lower case, searching across multiple lines, etc.

2015615120103606.jpg (690×247)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn