JavaScript Regular Expressions

Source: Internet
Author: User
Tags character classes expression engine

Overview:
In an article, when you want to find a string (consisting of more than one character) or a character, enter it in the search box and then search to get the result.
However, when you look for a string in a complex format, for example, when looking for the following string, start with "M", followed by two arbitrary characters, followed by an "n" character
, how do we enter the search string in the search box? After typing the first character "M" through the keyboard, immediately after any two characters, how to type?
This, uh, uh, uh .... , we're going to jam. At this point, we are going to search using regular expressions.
Regular expressions can search, replace, and manipulate arbitrary strings.


Metacharacters (metacharacters):
\: The backslash
^: the caret
$: The dollar sign
. : The period or dot
| : the vertical bar or pipe symbol
? : The question mark
*: The asterisk or star
+: the plus sign
(: The opening parenthesis
): The Closinig parenthesis
[: The opening square bracket
]: the closing square bracket
{: The opening curly brace
}: The closing curly brace

The above special characters are called metacharacters and have special meanings in regular expressions. If you want to search for these metacharacters in the text,
Then you must precede the metacharacters with a backslash (\) to tell the regular expression engine to treat the metacharacters as ordinary characters. For example:
You are looking for: 1+1=2, then your regular expression will be written: 1\+1=2


"." Period metacharacters
Matches only one character. This character is almost any character, such as letters, punctuation marks, numbers, underscores, etc., and is not case sensitive.
  Note that the search engine does a greedy match when the newline character "\ n" is not matched, and the period characters are written together with the "*" or "+" characters.
That means it matches as many characters as possible, which may not match your expectations.

"\w" metacharacters (lowercase)
Matches only one character. This character is a letter, a number, and an underscore.
  Note that you do not match punctuation marks, blank symbols
"\w" metacharacters (uppercase)
Matches only one character. This character is a \w character, which means the opposite of \w. In other words, matches any non-alphabetic, non-numeric, non-underlined character

"\d" metacharacters (lowercase)
Matches only one character. This character is a number from 0 to 9.

"\d" metacharacters (uppercase)
Matches only one character. This character is a \d character, which means the opposite of \d. In other words, it matches any non-numeric character.
"\s" metacharacters (lowercase)
Matches only one character. This character is a white space character, including a whitespace (), a tab (\ t), a newline (\ n)
"\s" metacharacters (lowercase)
Matches only one character. This character is a \s character, which means the opposite of \s, which is a non-whitespace character.


Character class (Character Classes or Character sets)
The character class is a number of unordered characters enclosed in "[]", in which the regular expression engine extracts a single character from the character class.

To make a match. If the expression is: mo[eao]n, then the search engine will be "Moen" or "moan" or "Moon" for pattern matching.
Character classes have some shorthand forms that represent a range of characters that must be contiguous, such as:
[A-z] means lowercase letters A to Z, meaning equivalent to [abcdefghijklmnopqrstuvwxyz]
[R-t] = lowercase letter R to T, meaning equivalent to [rst]
[A-z] means lowercase letters A to Z, meaning equivalent to [abcdefghijklmnopqrstuvwxyz]
[0-9] Represents a number, meaning with [0123456789] equivalent


"^" metacharacters
When the "^" metacharacters appear in the first position of the character class, the meaning of the inverse is expressed, for example:
[^ACM], which represents a character that matches a non-a or C or M.
When the "^" metacharacters appear elsewhere in the character class, the normal "^" character is represented, for example:
[A^CM], which represents a character that matches a or ^ or C or M.
When a "^" metacharacters appears in a non-character class, it matches the beginning of a string or line
"$" metacharacters
Matches the end position of a string or line
"\b" metacharacters
Matches the boundary of a word (appearing at the beginning or end of a word)
"\<" and "\>" metacharacters
Match the start and end position of a word
"()" Group introduction
Strings enclosed in parentheses "(ABC), (ABC|ABCDG)" are called a grouping.

Groups have some implicit variables ($, $, $ $, etc.) that can be used for subsequent substitutions, repetitions, and so on, for example:
The regular expression is: "((\w (\d{2})) (() (\d{2}))"
The text content of the search is: "A22 33"
Then the results of the match are as follows:
$, the reference is grouped as "((\w (\d{2})) (() (\d{2}))" and the matching result is: "A22 33"
$, the referenced group is "(\w (\d{2}))" and the matching result is: "A22"
$ $, the reference grouping is "(\d{2})" and the matching result is: "22"
$4, the referenced grouping is "(() (\d{2})", and the matching result is: "33"
$ $, the reference grouping is "()" and the matching result is: ""
$6, the referenced group is "(\d{2})" and the matching result is: "33"
application of Inverse reference in grouping
Sometimes you need to look for repetitive words, and then you need to reverse-reference the operation. A reverse reference, meaning to refer to a previously matched grouping.
For example: "M (o) \1n Lig (h|e) T", the first grouping is "(O)", followed by "\1" to indicate that the response refers to the matching result of the first grouping
The format of a reverse reference: a backslash plus a number, such as "\2", that refers to a second grouping
"?:" in the group of Applications
Indicates that the regular expression engine deletes the matching result of the "?:" Group when it returns the final matching result. Remove unwanted matches.
For example, "(The) (?: [EA]) (ON)", the result of the match is as follows,
$, the reference is grouped as "(The)"
$, the reference group is "(on)"
forward lookup and backward lookup
Note that the key to this app is that the sequence of characters that appears after the specified match is not returned by the regular expression engine.
(?: ...) , non-capturing group
(?= ...) , Positive forward Lookup
(?! ...) , Negative forward Lookup
(? <= ...) , the affirmative-type looks backwards
(? <! ...) , Negative search backwards
POSIX character class, [: Alnum:] equivalent to [a-za-z0-9],[:d Igit:] equivalent to [0-9], JS does not support this notation

JavaScript-supported meta-characters
    
JavaScript regular expressions are written in two ways
  
1, var reg = new RegExp (param1[, param2]);
The param1 parameter represents the pattern of the regular expression
The param2 parameter is optional and the value is one or more of "GMI",
G represents global, which is globally matched
M for multiline, multi-line matching
I represents ignoreCase and is not case-sensitive when matched
2, var reg =/[abc]{3}/[gmi];


Main methods of JavaScript regular Expression objects
1. Test ()
Test if a string matches a pattern? Returns true whenever there is a match, no match returns false;
2. EXEC ()
Gets a match from a string.
There are three methods of string objects in JavaScript that are used in conjunction with regular expressions
1. Match ()
Get the match, same as the Exec () method
2. Search ()
Find out if there is a match? Same function as test () method
3, replace () has two parameters,
The first parameter is a RegExp object,
The second argument is a string that is used to replace the found match

JavaScript Regular Expressions

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.