Javascript Regular Expression usage Summary

Source: Internet
Author: User
Usage of JavaScript Regular Expressions
    • Regular Expressions are powerful tools for pattern matching and replacement and can be applied in many languages. Javascript provides good support for regular expressions. Javascript has a regular expression object Regexp. This object provides a large number of attributes and methods to process regular expressions. At the same time, the string object also provides related methods to process regular expressions. In Web applications, regular Expressions are often used to verify the information entered by the user. For example, during user registration, the entered email address is valid. The following describes the basic usage of regular expressions.
Basic usage of JavaScript Regular Expressions
    • Regular Expressions are mainly used for three purposes:
      • Verify that the number conforms to the specified mode;
      • Use a regular expression to locate a specific text and then delete or replace the text;
      • Find and match the substring in the string by matching the pattern.
    • There are two methods to use regular expressions in javascript:
    • First, use the Regexp object
      • The basic syntax is:VaR RX = new Regexp (pattern [, flags]);The pattern parameter is a string that indicates the regular expression pattern to be used. The flag parameter is an optional string that indicates how to apply the pattern. You can use multiple tags in combination.
      • There are three main modes: G; all modes of full-text search; I: case-insensitive; M: multi-row search;
    • Second, use the regular expression to directly measure var RX =/pattern/flags;
      • Pattern indicates the regular expression mode used. How can flag apply the same flag as above;
    • Mode string
      • A regular expression is a text mode consisting of common characters and special characters (called metacharacters. The characters before the metacharacters are called delimiters. The metacharacters are categorized into delimiters and delimiters by function. The following describes the list of frequently used metacharacters.
Common metacharacters
Metacharacters Description
\ S Match a single space, including the tab key and line break
\ S Matches all characters except a single space character, and \ s are inverse operations.
\ D Match from 0 ~ 9 digits
\ W Match letters, numbers, or underscores
\ W Matches all characters that do not match \ W, and \ W and \ s are inverse operations.
\ CX Match the control characters specified by X. For example, \ CZ matches a Control-Z. The value of X must be ~ Z or ~ Z. Otherwise, C is considered as a desired 'C' character.
\ F Match a form feed, equivalent to \ x0c and \ Cl
\ N Match a line break, equivalent to \ x0a and \ CJ
\ R Match a carriage return, equivalent to \ x0d and \ cm
\ T Match a tab, equivalent to \ x09 and \ Ci
\ V Match a vertical tab. Equivalent to \ x0b and \ CK
. Used to match all characters except line breaks

Delimiter description
Qualifier Description
* Match the leading character Zero or multiple times
+ Match the leading character once or multiple times
? Match the leading character Zero or once
{N} Match the leading character n times, N> = 0;
{N ,} Match the leading character at least N times, N> = 0;
{N, m} Match the leading character at least N times, up to m times; m> N> = 0;

Operator description
Operator Description
^ The matching mode must start with the target string.
$ The matching mode must appear at the end of the target.
\ B The matching mode must appear at the beginning or end of the target string.
\ B The matched object must be within the boundary of the start and end of the target string. That is, the matched object cannot start or end of the target string.

    • Specifies the range of the regular expression matching mode.
      • [] The limited mode specifies the possibility of a character. For example,/[A-Z]/indicates matching any character from A to Z; /[A-Za-Z]/indicates matching any character from A to Z or from a-Z;
      • The ()-limited mode specifies that the content contained in () must appear in the object at the same time. For example,/(ABC) [0-9]/indicates any of the nine ABC1, abc2... abc9 in the target string, rather than Ab1 and A2;
      • "|" Is similar to the "or" operation in a logical operation, for example,/Sb | SBB | 6/indicates "SB", "SBB ", "6" matches any of them;
      • When the ^ character appears in [], it indicates a negative operation. For example,/[^ A-G]/indicates matching characters other than the A-G in the target object;
    • Character escape and priority order
    • To use metacharacters in the matching mode, you must use the Escape Character "\ + metacharacters". For example, you can use/12 \ */to represent the "12 *" mode; after constructing a regular expression, you can evaluate it like using a mathematical expression. Regular Expressions are carried out in the order from left to right in the matching process, and their operators have a certain priority order.

Priority Order
Priority Operator Description
1 \ Escape Character
2 (),(? :),(? =), [] Parentheses and square brackets
3 *, + ,?, {N}, {n ,}, {n, m} Qualifier
4 ^, $, \ Anymatchcharacter Location Order
5 | Or operation

Regular object expression object

    • Regexp object attributes and Methods

Static attributes of Regexp objects
Attribute Description
Index Read-only attribute. the starting position of the substring that matches the pattern for the first time in the string. The initial value is-1;
Input Read-only attribute. It returns the string currently used by the regular expression mode, which can be abbreviated as "$ _" and the initial value is an empty string;
Lastindex The read-only attribute returns the starting position of the substring that is successfully matched next time in the searched string. its initial position is-1, and its value will be modified with the matching;
Lastmatch Read-only attribute. It returns the last matched character of the regular expression in the search process, which can be abbreviated as "$ &";
Lastparen Read-only attribute, returns the child matching that is enclosed in square brackets in the regular expression during the search process;
Leftcontext Read-only attribute. It returns the characters between the start position and the last match in the searched string, which can be abbreviated as "$ ^ ";
Rightcontext Read-only attribute, returns the character from the last matched position to the end of the string in the searched string, which can be abbreviated as "$ '";
$1-$9 9 read-only attributes, such as $1, $2... $9, return 9 recently saved parts found during pattern matching;
Instance attributes of the Regexp object
Global Read-only attribute. A boolean value indicates whether g flag is set during Regexp creation. If G is set, true is returned; otherwise, false is returned;
Ignorecase Read-only attribute. A boolean value indicates whether the I flag is set when Regexp is created. If yes, true is returned; otherwise, false is returned;
Multiline A boolean value indicates whether the M flag is set when Regexp is created. If yes, true is returned; otherwise, false is returned;
Source Read-only attribute, string type, returns the regular expression pattern string;
Regexp object Method
Exec () The Exec () method of the Regexp object is a powerful method that matches regular expressions. The syntax format is: array#reobj.exe C (STR );
Test () The test () method of Regexp is used to test whether the regular expression matches a given string. The basic syntax is Var passed = reobj. text (STR); returns true if matching; otherwise, returns false
Compile () The compile () method of the Regexp object is used to compile the regular expression into an internal format for faster execution. The basic syntax is reobj. Compile (pattern [, Flag]).

Use of four regular expressions of a string object

four Regular Expression Methods of the string object
Search () used to find the special character string. Its basic syntax is Var STR = strobj. search (re); The method returns the first substring that matches the regular expression.
Replace () used for retrieval and replacement operations. The basic syntax format is strobj. replace (Re, STR); If Re is not a regular expression, it indicates replacement of a common string.
match () use the regular expression mode to search for strings. use VaR array = strobj. match (re); returns three attributes: 1) input contains the entire searched string, 2) index contains the position of the matched substring in the searched string, the lastindex attribute contains the position of the last matched string.
split () you can use a regular expression as a parameter to split a character string.
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.