JS Regular Expression Learning notes _ regular expressions

Source: Internet
Author: User
Tags character classes constructor lowercase modifiers

Regular expression: finds, replaces, and extracts operations on information in a string. (Comments and blanks are not supported, must be written on one line)
creation of regular expressions: characters contained between a pair of slashes (direct-amount syntax)
For example:

var pattern =/s$/;  Create a regular to match all strings ending with the letter S, and assign to pattern

One, character class
The character class is formed by putting the direct measure characters into square brackets alone.
A character class can match any of the characters it contains.
character classes for regular expressions:
[...] Any character within the square brackets
[^...] Any character not in square brackets
. Any character except line breaks and other Unicode line terminators
\wWords of any ASCII character are equivalent to [a-za-z0-9]
\wAny word that consists of non-ASCII characters is equivalent to [^a-za-z0-9]
\sAny Unicode whitespace characters
\sAny non-Unicode blank character
\dAny ASCII number is equivalent to [0-9]
\dAny non-ASCII number is equivalent to [^0-9]
[\b]BACKSPACE Direct Quantity (special case)
For example:/[The a,b,c]/representation matches any one of the letters A,B,C.
/[\s\d]/to match any white space character or number

Second, repeat
The token used to indicate that the specified character repeats
Repeating character syntax for regular expressions:
{n,m} matches the previous item at least n times, but not more than m times
{n,} matches n or more times before
{n} matches previous n times
? matches 0 or 1 times before, that is, the previous item is optional, equivalent to {0,1}
+ matches 1 or more times before, equivalent to {1,}
* matches the previous item 0 or more times, equivalent to {0,}

For example :/\d{2,4}/match 2~4 number
/\w{3}\d/exactly matches 3 words and an optional number

Iii. Select, group, and reference
Matching programs: From left to right, if the match on the left matches, automatically ignores the right match (even if a better match can be produced)
①| Used to separate the characters for selection
For example:/ab|cd|ef/can match the string "AB", "CD" or "EF"
/\d{3}| [A-z] {4}/matches 3 digits or four lowercase letters
② ()
function One:To synthesize a subexpression with a single item
function Two:To define a child pattern in a complete pattern
When a regular expression succeeds and the target string matches, the part that matches the child pattern in the parentheses can be extracted from the target string.
For example:
/[a-z]+\d+/retrieves one or more lowercase letters followed by one or more digits
If we are concerned with the number of each tail, you can put the numeric portion of the pattern into parentheses (/[a-z]+ (\d)/) so that you can extract the number from the retrieved match.
function Three:Allows the preceding subexpression to be introduced at the back of the same regular expression
Implementation method: Add one or more digits after character \ (this number specifies the position of the parenthesized subexpression in the regular expression)
For example: \1 refers to the first parenthesized subexpression
Note: The position is subject to the left parenthesis position of the participation count;
The regular expression does not allow single quotes to be enclosed in double quotes, and vice versa.
Summarize:
| Select to match the subexpression to the left of the character or to the right side of the child expression
(...) Group, combining several items into one unit
(...?) Combine items into one unit, but do not memorize characters that match the group
\ nMatches the first match of the Nth group, the group is a subexpression in parentheses, and the group index is a left to right parenthesis number, (?: This form of grouping does not participate in index encoding
Four, specify the location of the match:
Anchor:
Specify a valid location for the match to occur
^Match the beginning of a string
$Match end of string
\bMatch the bounds of a word (located between \w and \w)
\bPosition that matches a non-word boundary

Five, modifiers

Modifiers are placed outside of//, and do not appear between two bars/lines
i do not case sensitive
G Global match, find all matches, not find the first one and then stop
m multiple-line matching

Vi. methods for pattern-matching string objects
method One: search () retrieve the location to match
Parameters: a Regular expression
Returns the starting position of the first substring to match, if no matching substring is found, return-1
For example:

<script>
 "JavaScript". Search (/script/i);  The return value is 4
</script>

Note: ① if the search () parameter is not a regular expression, it is converted to a regular expression through the RegExp constructor
②search () does not support global retrieval
method Two: replace () perform retrieve and replace operations
Parameters: The first--> regular expression
The second--> the string to be replaced (also a function that can dynamically compute the replacement string)
Note: ① supports G,
G: All substrings matching the pattern will be replaced with the string specified by the second argument
G is not set in regular expression: replace only the first substring that matches
② If the first argument is not a regular, but a string, replace () will search for the string directly

For example, replace all case-insensitive JavaScript with the correct capitalization javascript

<script>
 var str = "Javascript,javascript,javascript,javascript";
 Alert (Str.replace (/javascript/gi, "JavaScript"));  Javascript,javascript,javascript,javascript
</script>

method Three: match ()
Parameters: Regular Expressions
Returns: An array of matching results
Support G
Example One:

<script>
 var math = "1 plus 2 equals 3". Match (/\d+/g);
 Console.log (math);  ["1", "2", "3"]
 Console.log (typeof math);  Object
</script>

Example two:

<script>
 var math = "1 plus 2 equals 3". Match (/\d/);
 Console.log (math);  ["1", index:0, Input: "1 plus 2 equals 3"]
 Console.log (typeof math);  Object
</script>

Attention:
Pass in a Non-global regular expression to the math () of the string:
Match: Retrieve only the first match
return value: Array
The first element of the array: the matched string
The second element of the array: a subexpression-enclosed expression in a regular expression with two attributes-->index and input

method Four: split ()
return value: Array
Separator: parameter of Split ()
For example:

<script>
 var str= "1,2,3,4,5,6,7,8". Split (', ');
 Console.log (str); ["1", "2", "3", "4", "5", "6", "7", "8"]
</script>

parameter is a regular expression (you can specify a separator, allowing any number of whitespace characters to be left on either side)
Return: The string that is used to call it is split into an array of substrings

<script>
 var str= "1,2,3,4,5,6,7,8". Split (/\s*,\s*/);  Allow any number of whitespace characters
 Console.log (str) to be left on both sides;  ["1", "2", "3", "4", "5", "6", "7", "8"]
</script>

Vii. REGEXP Objects

RegExp () constructor:
Parameters: two string parameters
First: The body part of the regular expression (text between two slashes)
Second: (optional) Specifies the modifier of the regular expression (g,i,m the combination of these three or three)
Note: Both string literals and regular expressions use/characters as prefixes for translation characters
For example:

var zipCode = new RegExp (' \\d{5} ', ' G '); 5 digits in the global match string, note that this is//not

The above is for everyone to arrange for the JS regular expression of learning notes, I hope to learn more JS regular expression help.

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.