Relive regular expressions in JavaScript JS Learning notes _ Regular expressions

Source: Internet
Author: User
Tags alphanumeric characters
First, create regular expressions

Creating regular expressions is similar to creating strings, and creating regular expressions provides two methods, one is to take the new operator and the other is to use a literal method.

Copy Code code as follows:

var dog = new RegExp (' dog '); First argument string
var dog = new RegExp (' dog ', ' ig '); Second parameter optional pattern modifier

var dog =/dog/;
var dog =/dog/ig; Literal way.


The RegExp object contains two methods: Test () and exec (), which are similar in functionality and are used for testing string matching.

The test () method finds in the string whether a specified regular expression exists and returns a Boolean value, returns True if it exists, and returns false if it does not exist.

The exec () method is also used to find a specified regular expression in a string, and if the Exec () method succeeds, returns an array of related information that contains the lookup string. If execution fails, NULL is returned.

Although it is simpler to create regular expression objects, the real complication is the pattern information described in regular syntax. Next, we'll explore some of the patterns in several sections:

Second, the regular expression method of string

For example:

Copy Code code as follows:

var Mypattern =/dog/ig;var Thestr = "This is a dog,that is a dog!" Console.log (Thestr.match (Mypattern)); ["Dog", "dog"]
var Mypattern =/dog/i;var Thestr = "This is a dog,that is a dog!" Console.log (Thestr.search (mypattern))//10
var mypattern =/dog/i; Note that the global is not set, and if the global setting is set, the result is: ' is a ' cat,that is a cat!var thestr = ' This is a dog,that is a dog! ' Console.log (Thestr.replace (Mypattern, "Cat")); This is a cat,that is a dog!
var Mypattern =/\s/ig;var Thestr = "This is a dog,that is a dog!" Console.log (Thestr.split (Mypattern)); [' This ', ' is ', ' a ', ' dog,that ', ' is ', ' a ', ' dog! '], this plus no global g has no effect!

second, the direct measure character
In regular expressions, all alphabetic characters and numbers are matched by the direct amount. Also, regular expressions support some non-alphanumeric characters by using a backslash "\" plus letters. See the following list of matches:
Character matching
Letters and numbers themselves, for example:/a/matches the letter A
\o nul characters
\ t tab
\ n Line Feed
\v Vertical Tab
\f Page Breaks
\ r Carriage Return

three, character class

The character class is formed by placing individual direct characters in []. A character class matches any character it contains.

For example:/[abc]/matches any one of the ABC three letters.
You can also define a negative character class. Take advantage of the ^ character. For example:/[^abc]/matches all characters except a B C.
Alternatively, you can determine the range of a character by using a ligature symbol. For example:/[a-z]/matches A to Z. To match all Latin alphabet sets, you can use the/[a-za-z0-9]/
Character matching

. Any character except line breaks and other Unicode line terminators
\w any ASCII word character, equivalent to [a-za-z0-9_]
\w any non-ASCII word character, equivalent to [^a-za-z0-9_]
\s any Unicode whitespace characters
\s any non-Unicode whitespace characters
\d any ASCII number, equivalent to [0-9]
\d any non-ASCII number, equivalent to [^0-9]
[\b] Backspace direct quantity
Note that the above escape sequence can also be used within brackets []. For example,/[\s\d]/matches any blank character or number.

Iv. Duplication

According to the knowledge involved in the second section, we can express the two digits as/\d\d/, but if the number of repetitions is too high, it must not be written.
Character matching
{N,m} matches the previous item at least n times, but not more than m times
{N,} matches the previous item is greater than or equal to n times

{n} exactly n times
? 0 or 1 times,
+ 1 or more times, equivalent to {1,}
* 0 or more times
For example:/\d{2,4}/2, 3, or 4 digits. /\w{3}\d?/matches 3 characters and has an optional number.
Note:/a*/actually matches bbbb because 0 or more a matches bbbb.

Five, anchor characters

vi. selection, grouping, and referencing

1, character "|" Used to separate the selected characters. For example:/ab|cd|ef/matches a string ab or CD or EF. /\d{3}| [A-z] {4}/matches 3 digits or 4 lowercase letters

2, "()" function is to separate the project group to synthesize the subexpression. For example:/java (script)?/Match Java, there can be script later, also can not.

Note that in a group, $, $, and $ $ represent the contents of the first few groupings, respectively.

For example:

var mypattern =/(dog). * (cat)/;var THESTR = "This is a dog,that is a cat!" Mypattern.exec (THESTR); Console.log (regexp.$2 + "" + regexp.$1); Cat Dog Console.log (Regexp.lastmatch); Last match: Dog,that is a cat

Vii. greed and inertia


For example:

Copy Code code as follows:

var Mypattern =/[a-z]+/;var Thestr = "This is a dog,that is a cat!" Console.log (Thestr.replace (Mypattern, "a")); A is a dog,that is a cat! Greedy, the This all match!
var Mypattern =/[a-z]+?/;var Thestr = "This is a dog,that is a cat!" Console.log (Thestr.replace (Mypattern, "a")); Ahis is a dog,that is a cat! Not greedy, only to match the T in this!

Finally, the tolocalestring () and ToString () methods inherited by the RegExp instance return the literal amount of the regular expression, that is/abc/

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.