Introduction to JavaScript Regular expressions

Source: Internet
Author: User

A regular expression is an object that is used to describe a character pattern. It is used to perform pattern matching (pattern-matching) and "Find-replace" (search-and-replace) tasks in text. The style of JavaScript is similar to that of Perl.

Create

var reg = new RegExp(pattern,modifiers);
Or
var reg = /pattern/modifiers;

Modifiers includes

    • G: Performs a global match (finds all matches rather than stops after the first match is found)
      Such as
      ‘Hey, Is this all there Is ‘.replace(/Is/g, ‘is‘);//替换某字符串中所有的Is为is
    • I: Perform a case insensitive match
    • M: Perform multi-line matching, see here
      PS When you create a new regular, you can have multiple modes, such as/hEllO/ig
The regular correlation method
    • . Test (). This method matches a string. and returns TRUE or false based on the matching result. For example
      /e/.test(‘The best things in life are free‘); //true
    • . EXEC (). The method will match a string. and returns the first matching item. For example:
      /e/.exec(‘The best things in life are free‘); // ["e"]

In the string method, there is a match method, exec similar to. Such as

‘The best things in life are free‘.match(/e/); //["e"]
Meta-characters in regular

Metacharacters in regular expressions is a special character used to replace a class of characters with the same attributes, which can also be called a character class (character Class).

    • . Finds a single character, in addition to line breaks and line terminators.
    • \w Find Word characters (letters, numbers, and underscores "_").
    • \w Find non-word characters.
    • \d find numbers.
    • \d to find non-numeric characters.
    • \s Find white space characters.
    • \s find non-whitespace characters.
    • \b matches the word boundary.
    • \b Matches a non-word boundary.
    • To find the NUL character.
    • \ nto find newline characters.
    • \f Find a page break.
    • \ r to find the carriage return character.
    • \ t to find a tab.
    • \v Find vertical tabs.
    • \xxx finds characters that are specified in octal number XXX.
    • \XDD finds the characters specified in hexadecimal number DD.
    • \uxxxx finds Unicode characters that are specified in hexadecimal number xxxx.
Curly braces () in regular expressions

To group. Like an email address, we can divide it into

    1. @ the front part;
    2. @ behind '. ' The previous part;
    3. ’.’ The next part;
      If you want to match all Gamil mailboxes in the user name before @ with Jack in the replace with Joel
      ‘[email protected]‘.replace(/^(\w*)(jack)(\w*)@gmail\.com$/, ‘[email protected]‘);  //"iamjo[email protected]"
      This modularity-like idea not only allows us to focus on finding a small part of it at a time, but also allows us to easily replace one of those parts in the back, without having to struggle with the "reaching" pain.
square brackets []

Used to find characters in a range

    • [ABC] finds any character between square brackets.
    • [^ABC] finds any characters that are not in brackets.
    • [0-9] Find any number from 0 to 9.
    • [A-z] finds any characters from lowercase a to lower Z.
    • [A-z] finds any characters from uppercase A to uppercase Z.
    • [A-z] finds any characters from uppercase A to lowercase z.
    • [ADGK] finds any character in square brackets.
    • [^ADGK] finds any characters that are not in square brackets.
Quantifiers

Match multiple. If a match is matched to 3 numbers available /\d{3}/ .

    • n+ matches any string that contains at least one n.
    • n matches any string that contains 0 or more N.
    • N? Matches any string that contains 0 or one n.
    • N{x} matches a string containing a sequence of X N.
    • N{x,y} matches a string containing a sequence of X or Y N.
    • N{x,} matches a string that contains at least X N of a sequence.
    • n$ matches any string that ends with N.
    • ^n matches any string that begins with N.
    • =n matches any string immediately following the specified string n.
    • ?! n matches any string that does not follow the specified string n immediately thereafter.
Exercises
    1. Matches the number with a decimal point in the string "1.5 0 123-7-0.4", either positive or negative.
    2. Matches a number ([1.5,0,123]) in the string "1.5 0 123", whether it is an integer or a decimal.
    3. More Exercises
Advanced greedy and non-greedy modes

The default regular match is greedy mode, that is, the preceding regular matches as many as possible. Such as

/(\d+)(\d+)/.exec(‘12345‘);//结果["12345", "1234", "5"]

Open non-greedy mode, after the quantifier is added?
Such as

/(\d+?)(\d+)/.exec(‘12345‘);//结果["12345", "1", "2345"]

In a more detailed explanation, Dot here

Non-capturing grouping

The contents of the parentheses ?: begin with. Such as:

/(?:\d+)\d+/.exec(‘123‘);// 结果 ["123"]
The

Lookaround is a generic term for forward matching (Lookahead) and backward Matching (lookbehind).
Match forward

Including forward positive matching (Positive Lookahead) and forward negative matching (negative Lookahead), the syntax is ?= and ?! .
Forward match: Matches any string followed by the specified string n.
Forward negative match: matches any string that does not follow the specified string n immediately thereafter.

Similarly, there are backwards matches.
Only Lookahead is currently available in JavaScript, and Lookbehind is not available.

Lookaround Reference Tutorial: http://www.regular-expressions.info/lookaround.html

Regular Expansion Library

Xregexp characteristics

    1. Supports all ES5 of the regular syntax.
    2. Compatible Explorer 5.5+, Firefox 1.5+, Chrome, Safari, and Opera 11+. can also be used on Nodejs.
    3. Higher readability than native.
    4. It is easier to use than native.
More Resources
    • Regular Concise reference
Reference
    • Http://www.w3school.com.cn/js/jsref_obj_regexp.asp
    • Http://www.html-js.com/article/A-day-to-learn-JavaScript-JavaScript-regular-expressions-a
    • Http://javascript.info/tutorial/regular-expressions-javascript
    • Https://github.com/lifesinger/lifesinger.github.com/issues/162#wechat_redirect
Exercise Answer
    1. ' 1.5 0 123-7 -0.4 '. Match (/(-?\d+.\d+)/g)
    2. ' 1.5 0 123 '. Match (/(\d+.? \d+) |0/g)
    recommended Expand Reading

    Introduction to JavaScript Regular expressions

    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.