Regular expression summary in JavaScript __java

Source: Internet
Author: User
Tags modifier modifiers

Methods that define regular expressions have two methods for defining regular expressions: The constructor definition and the regular expression direct quantity definition. For example:

var reg1 = new RegExp (' \d{5, 11} '); Define
var reg2 =/\d{5 by constructor, 12}/;//through the direct quantity definition
Regular Expression direct measure character \o:nul character (\u0000) \ t: Tab (\u0009) \ n: newline character (\u000a) \v: Vertical tab (\U000B) \f: Page break (\u000c) \ r: Carriage return (\u000d) \ XNN: Latin characters specified by the hexadecimal number NN, for example, \x0a equivalent to \ n \uxxxx: Unicode characters specified by the hexadecimal number XXXX, for example, \u0009 equivalent to \ t \cx: Control character ^x, for example, \CJ equivalent to a newline character \ n Regular expression anchor character ^ : Matches the beginning of a string, in multiple-line retrieval, match the beginning of a line $: match the end of a string, in multi-line retrieval, match the end of a line \b: match the bounds of a word, in short, the position between the character \w and \w, or the position between the character \w and the beginning or end of the string ([\b Match backspace) \b: a position that matches a non-word boundary (? =p): 0 wide forward lookahead assertion, which requires that the next character be matched to p, but cannot include those characters (.!p) that match p: 0 wide negative lookahead assertion, requiring that the next string not match the character class of the regular expression with P [...] : Any character in square brackets [^ ...] : Any character not in square brackets.: Any character other than a newline and other Unicode line terminator \w: A word that consists of any ASCII character, equivalent to [a-za-z0-9] \w: Any word that is not an ASCII character, equivalent to [^a-za-z0-9] \ S: Any Unicode whitespace \s: Any non-Unicode whitespace character, note \w and \s different \d: Any ASCII number, equivalent to [0-9] \d: Any character other than the ASCII number, equivalent to [^0-9] [\b]: Backspace direct amount (special case) Repeating character syntax for regular expressions {n, m}: matches the previous item at least n times, but not more than M {N,}: match the previous n or more times {n}: match the previous n times?: Matches the previous item 0 or 1 times, that is, the previous item is optional, equivalent to {0, 1} +: matching the previous 1 or more times, Equivalent to {1,} *: Match previous 0 or more times, equivalent to {0,} Select, group, and reference character of regular expression |: Select, match the subexpression on the left of the symbol or the subexpression on the right (...) : Group, combining several items into one unit, which can be passed "*", "+", "?" and "|" , and you can remember the strings that match this group for any subsequent use (?: ...) : group only, combine items into one unit, but do not memorize characters that match the reorganization \ n: and nth GroupMatches a matching character, the group is a subexpression (and possibly nested) in parentheses, the group index is the number of left to right parentheses, and the "(?:" Form of grouping does not encode regular expression modifiers I: Performs case-insensitive matching G: Performs a global match, in short, finds all matches, Instead of stopping m after finding the first one: multiple-line matching pattern, ^ matches the beginning of a line and the beginning of a string, $ matches the end of a row and the end of a string method search (): its argument is a regular expression that returns the starting position of the first substring to match. Returns-1 if there is no matching substring. If the search () parameter is not a regular expression, it is first converted to a regular expression through the RegExp constructor, and search () does not support global retrieval because it ignores modifier g. Such as:
var s = "JavaScript". Search (/script/i); s = 4
Replace (): It is used to perform retrieval and substitution. Receives two parameters, the first is a regular expression, and the second is the string to be replaced. If the regular expression is set modifier g, it is replaced globally, otherwise only the first substring of the match is replaced. If the first argument is not a regular expression, the string is searched directly, rather than converted to a regular expression. Such as:
code from http://caibaojian.com/javascript-zhengze.html
var s = "JavaScript". Replace (/java/gi, "Script");//S =< C1/>script Script
Match (): its argument is a regular expression, and if not, the RegExp conversion returns an array of matching results. If modifier g is set, a global match is made. Such as:
var d = ' ff/hh '. Match (/\d+/g); D = ["55", "33", "77"]
Split (): This method is used to split the string that calls it into an array of substrings, using a split () parameter, and its arguments can also make a regular expression. Such as:
var d = ' 123,31,453,645 '. Split (', '); d = ["123", "the", "453", "645"]
var d = ', 123, 3,,, and  21 '. Split (/\s*,\s*/); 123 "," 44 "," 64 "," 67 "," 3 "]
The RegExp object has 5 properties per RegExp object. The property source is a read-only string containing the text of the regular expression. The property Global is a read-only Boolean value that indicates whether the regular expression has a modifier g. The property ignorecase is a read-only Boolean value indicating whether the regular expression has modifiers I. The property multiline is a read-only Boolean value indicating whether the regular expression has a modifier m. The property lastindex is a readable, writable integer that stores the start of the next retrieval of the entire string if the matching pattern has a G modifier. The RegExp object has two methods. The exec () parameter is a string that functions like match (), and the Exec () method executes a regular expression on a specified string, which is the execution of a matching search in a string. Returns null if no match is found, returns an array if a match is found, the first element of this array contains a string that matches the regular expression, and the remaining elements are substrings that match the subexpression within the parentheses, and the same array is returned regardless of whether the regular expression has a modifier g. When a regular expression object that calls exec () has a modifier g, it sets the Lastindex property of the current regular expression object to the character position that matches the substring next to it. When the same regular expression invokes exec () The second time, it retrieves from the string indicated by the Lastindex property, and if EXEC () does not find any matching results, it resets the lastindex to 0. Such as:
var p =/java/g;
var text = "JavaScript is more fun than java!"
var R;
while (r = p.exec (text))!= null) {
       Console.log (R, ' lastindex: ' + p.lastindex);
}
Another method is test (), its argument is a string, a string is checked with test (), or false if a matching result of a regular expression is included, returns True. Such as:
var p =/java/i;
P.test (' JavaScript '); True
Original link: JavaScript in the regular expression summary copyright, reprint, please specify the source, offenders must investigate.
Annotated source format: Front-End development blog (http://caibaojian.com/javascript-zhengze.html)
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.