Javascript Regular Expression definition (syntax) Summary, javascript Regular Expression

Source: Internet
Author: User
Tags character classes

Javascript Regular Expression definition (syntax) Summary, javascript Regular Expression

This article describes the definition (syntax) of javascript regular expressions ). We will share this with you for your reference. The details are as follows:

Two defining methods of Regular Expressions: one is to call RegExp () directly, and the other is to define it directly using the literal, that is, var re =/regular expression /;

The two definition methods call the RegExp () method in essence.

When calling the same regular code segment, ECMAScript3 and ECMAScript5 behave completely differently.

function reg(){ var re = /\sjavascript/; return re;}

The reg () method is called multiple times in ECMAScript3 and ECMAScript5 respectively.

In ECMAScript3, the same RegExp object is called. In ECMAScript5, different RegExp objects are called. Because each execution in EXCMAScript5 generates a new RegExp object.

Therefore, ECMAScript3 may cause program risks, because as long as this object is modified in one place, all calls to this object will change.

1. directly count characters

Generally, regular expressions directly match characters, such

/Javascript/
Will Directly match the characters in javascript

It also supports non-letter character matching, such:

\ O NUL character (\ u0000)

\ T tab (\ u0009)

\ N linefeed (\ u000A)

\ V vertical tab (\ u000B)

\ F page feed (\ u000C)

\ R carriage return (\ u000D)

\ Xnn is a Latin character specified by the hexadecimal number nn. For example, \ x0A is equivalent to \ n.

\ Uxxxx is a Unicode Character specified by the hexadecimal number xxxx. For example, \ u0009 is equivalent to \ t.

\ CX control character ^ X, for example, \ cJ is equivalent to line break \ n

In regular expressions, there are some punctuation marks with special meanings that need to be escaped '\'.

^ $. * +? =! : | \/() [] {}

2. character classes

[...] Any character in square brackets

[^...] Any character not in square brackets

. Any character

\ W any word consisting of ASCII characters, equivalent to [a-zA-Z0-9]

\ W any word that is not comprised of ASCII characters, equivalent to [^ a-zA-Z0-9]

\ S any Unicode white space character

\ S any non-Unicode characters. Note that \ w and \ S are different.

\ D any ASCII value, equivalent to [0-9]

\ D any character except ASCII numbers, equivalent to [^ 0-9]

[\ B] Return direct quantity (Special Case)

3. Repeat (times)

? 0 or 1 time

+ 1 or multiple times

* Any time

{N} n times

{M, n} minimum m times, maximum n times

{N,} n or more times

Regular Expressions are greedy by default.

For example, if [a + B +] is to match aaabb, it will not match AB and aab, but will only match aaabb.

[A +? B ++] Why does this difference occur when it matches aaab?

A: +? Is to make the regular expression non-Greedy match, then B will only match one B here, Then why will a match three? This is because regular expression pattern matching always finds the first possible position in the string.

4. Option | group | reference

| It is used to separate available characters, such as [AB | cd]. It can match both AB and cd. Note: The matching order of the selected items is left to right, therefore, [a | AB], if a matches successfully, it will not match AB, even if AB is a better match.

() 1. Use a separate item as a subexpression/java (script )? /The subexpression can be formed by matching javascript and java, that is, the parentheses. The subexpression can be executed. | *? And other operations

2. define the expression after the child mode in the complete mode that can reference the parentheses/(['"]) [a-z] \ 1/\ 1 references the expression in the first parentheses, so it references ['"]

3. note: /['"] [a-z] ['"]/This regular expression indicates that a single or double quotation mark is added with a lowercase letter and a single or double quotation mark, the single double quotation marks before and after are not matched. If you want to match, you can write [(['"]) [a-z] \ 1] in this way.

\ Add a number to reference the expression in the parentheses

5. Determine the matching position (Anchor)

^ Match the start of a string. In multi-row search, match the beginning of a line

$ Match the end of a string. In multi-row search, match the end of a row.

\ B matches the boundary of a word. In short, it is located between the character \ w and \ W, or between the character \ w and the start or end of the string.

\ B matches the position of a non-word boundary

(? = P) Zero-width forward assertions require that all subsequent characters match p, but not those that match p

(?! P) Zero-width-negative predicate, requires that the subsequent characters do not match p

6. Modifier

Written in regular expression literal // on the right

I. Perform case-insensitive matching.

G executes a global match. In short, it finds all the matches instead of stopping them after finding the first one.

M multi-row matching mode. ^ matches the beginning of a row and the start of a string. $ matches the end of a row and the end of a string. java $/m can match java \ nfunc.

Note: When the regular expression is global, the current lastIndex is set to the current position every time exec () and test () are performed, the execution starts from the location of the lastIndex when the execution is performed again. Therefore, it is best to set the lastIndex to 0 at each execution.

I hope this article will help you design JavaScript programs.

Articles you may be interested in:
  • Common functions of js Regular Expressions (continued)
  • Common functions of js Regular Expressions
  • Js Regular Expression learning notes
  • 12 common js Regular Expressions
  • Share the JavaScript regular expression)
  • Learn JavaScript Regular Expressions
  • Verify the email address using the js Regular Expression
  • How to remove double quotation marks using a JavaScript Regular Expression
  • Detailed description of the use of regular expressions in JavaScript programming
  • Collect Regular Expressions for JS mailbox Verification

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.