Regular Expressions in JavaScript determine matching rules and common methods,

Source: Internet
Author: User

Regular Expressions in JavaScript determine matching rules and common methods,

String is the most involved data structure during programming, and the demand for string operations is almost everywhere. Regular Expressions are powerful weapons used to match strings. Its design concept is to define a rule for a string in a descriptive language. Any character string that complies with the rule will be considered "matched.

\ D can match a number '00 \ d' and can match '007 '.' \ d \ d' can match '010'
\ W can match a letter or number '\ w \ W' can match 'js'
\ S can match a space (including tabs and other blank characters)
\ DWS indicates the opposite Matching Relationship Between the match and lower-case letters.
\ Number n indicates a reference to the string text captured by the nth group, which can be matched again/(\ d +) (0 *) \ 1/can match '123'
. Any character 'js. 'can be matched with 'jsp', 'jss', and 'js! '

Regular Expression quantifiers:

* Represents any number of characters (including 0)
+ Indicates at least one character.
? 0 or 1 Character
{N} represents n characters
{N, m} represents n-m characters

A | B can match A or B (J | j). ava (S | s) matches 'javascript ', or 'javascript'

^ Indicates the beginning of a row ^ \ d indicates that it must start with a number

$ Indicates the end of a row \ d $ indicates that the row must end with a number.

[] Indicates the range and character class

[A-zA-Z \ _ \ $] [0-9a-zA-Z \ _ \ $] * can match a string starting with a letter or underscore or $, followed by any string consisting of digits, letters, underscores, and $, that is, variable names allowed by JavaScript.

[A-zA-Z \ _ \ $] [0-9a-zA-Z \ _ \ $] {0, 19} more precisely, the variable length is limited to 1-20 characters (the first 1 character + the last 19 characters at most)

[] ^ Indicates to exclude a character [^ #?] Besides? And # All characters

() Indicates a capturing grouping. You can group matching strings and extract substrings.

If a group is defined in a regular expression, you can use the exec () method to extract the substring from the RegExp object.

After successful exec () matching, an Array is returned. The first element is the entire string matched by the regular expression. The subsequent string indicates the matched substring.

If exec () fails to match, null is returned.

^ (\ D {3})-(\ d {3, 8}) $ defines two groups respectively. You can extract the area code and local number from the matching string directly:

var re = /^(\d{3})-(\d{3,8})$/;re.exec('010-12345'); // ['010-12345', '010', '12345']re.exec('010 12345'); // null 

(? :) When a group does not want to be captured, the program execution speed can be improved. Non-capturing groups do not interfere with the number of capturing groups.

Splitting strings using regular expressions is more flexible than using fixed characters

'a,b;; c d'.split(/[\s\,\;]+/); // ['a', 'b', 'c', 'd']  

The test () method of the RegExp object is used to test whether the given string meets the conditions.

var re = /^\d{3}\-\d{3,8}$/;re.test('010-12345'); // truere.test('010-1234x'); // false

It should be noted that by default, regular matching is greedy, that is, matching as many characters as possible.

var re = /^(\d+)(0*)$/;re.exec('102300'); // ['102300', '102300', '']

Because \ d + uses greedy match, all the following 0 is matched directly, and the result 0 * can only match null strings.

ADD? We can make \ d + adopt non-Greedy match:

var re = /^(\d+?)(0*)$/;re.exec('102300'); // ['102300', '1023', '00'] 

Special logo

G flag, indicating global match

I flag, indicating case-insensitive

M flag, indicating multi-row matching

The Escape Character '\' must be used for 15 special characters.

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

Summary

The above section describes the regular expressions in JavaScript to determine matching rules and common methods. I hope this will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.