Introduction to javascript regular expressions, regular expressions

Source: Internet
Author: User

Introduction to javascript regular expressions, regular expressions

Overview

A regular expression is a set of rules that are used to match characters in a string.

Basic syntax

Metacharacters

In regular expression mode, some characters have special meanings and are called metacharacters. Metacharacters are matched for a single character.

\ W matches any one of the upper and lower case English characters and numbers 0 to 9 and underline, equivalent to [a-zA-Z0-9 _]

\ W does not match any one of the upper and lower case English characters and numbers 0 to 9, equivalent to [^ a-zA-Z0-9 _]

\ S matches any blank characters, which is equivalent to [\ f \ n \ r \ t \ v]

\ S matches any non-blank characters, which is equivalent to [^ \ s]

\ D matches a single number between 0 and 9, which is equivalent to [0-9]

\ D does not match any single number between 0 and 9, which is equivalent to [^ 0-9]

[\ U4e00-\ u9fa5] matches any single Chinese character (here it uses Unicode encoding to represent Chinese characters)

Identifier

The common Regular Expression Delimiter is a double slash, regex, or/

Atomic Concept

The atoms in a regular expression are divided into visible atoms and invisible atoms.

[\ F \ n \ r \ t \ v] is an invisible atom. Others indicate visible atoms.

Quantifiers

\ * Matches 0 to multiple metacharacters, equivalent to {0 ,}

? Matches 0 to 1 metacharacters, equivalent to {0, 1}

{N} matches n metacharacters

{N,} matches at least n metacharacters

{N, m} matches n to m metacharacters

\ + Matches at least 1 metacharacter, equivalent to {1 ,}

Boundary

\ B match word boundary

^ The string must start with a specified character

$ The string must end with a specified character

Capture Group

In a regular expression, a number of units (which can be characters or regular expressions) are grouped together to form an independent unit.

In a regular expression, a group is divided into a capture group and a non-capture group.

/(pattern)/flags

Pattern correction

Greedy/lazy, case insensitive, ignore blank characters

Use Cases

Form Verification, template engine

The above is all the content of this article. I hope you will like it.

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.