Basic usage of JS regular expressions

Source: Internet
Author: User

REGEXP (regular Expression) object

A regular expression is an object that describes a character pattern and can handle more complex strings. Make a match substitution.

Common modifiers:
I/m/g

How to use:
"Declaration method One: New RegExp (value)"
var Patt = new RegExp (value), and the values you want to check are placed in "value"
var res = Patt.test ("This is JavaScript course"); Detects if there is a value in "value" in this paragraph, and the return value is a Boolean type.

"Declaration method Two: var Patt =/value/"
var Patt =/value/i: I means case insensitive


"Test () function: Detects if there is a match for the regular expression pattern in the string, the return type is a Boolean type"
Parameter: String specified by the strings.
Note: Each time the test () function is executed, it finds only one match at most once, and returns True, otherwise false.

"square brackets are used to find values within a range:/[value]/"
var res =/[abc]/.test ("def") return value: Returns True if the value in Test ("value") "exists" in/[abc]/, otherwise false

The ^ symbol indicates that the lookup no longer has any value in that range/[^value/] "
var res =/[^abc]/.test ("def") return value: Returns True if the value in Test ("value") is "not present" in/[abc]/, otherwise false


"Meta-character: It's logic is or logic, as long as a true result is true"
"Find single characters, except line breaks and line terminators:/./"
Example:
res =/./.test ("\ n"); Returns false
res =/./.test ("This is a test\n") returns True

"Find word Character: \w lowercase"
Example:
res =/\w/.test ("!#@"); Returns false because the word character "a-za-z0-9" is not included in test ("value")

"Find non-word characters: \w uppercase"
Example:
res =/\w/.test ("!# @abc"); Returns true because Test ("value") contains a non-word character, its logic is or logic, as long as a true knot
The fruit is true.

"Find white space characters: \s"
Example:
Res=/\s/.test (' Hello World ');


"Find non-whitespace characters: \s"
Example:
Res=/\s/.test ("");

"Match word boundary: \b Word boundary refers to the left and right sides of a word, beginning and ending"
Example: usually do not write on both sides
res =/\bvalue/.test (' good '); Returns true Value after \b Indicates whether the following value is on the leftmost
res =/value\b/.test (' good '); Returns true Value before \b Indicates whether the preceding value is on the far right

"Match non-word boundary: \b Word boundary refers to the left and right sides of a word, beginning and ending"
Example: Value values in the left and right sides of \b do not matter
res =/o\b/.test (' good '); Returns True because O this value is no longer on both sides of the test string

"Quantifier +-*/?" 】
"/value+:+ represents values in value, matching at least one or more times"
Example:
Res=/o+/.test (' Google ');

"/value*:* represents values in value, matches >=0 times"
Example:
Res=/o*/.test (' Google ');

"/value?:?" Represents values in value, matching 0 or one time "
Example:
Res=/o?/.test (' Google ');

"/^value/: ^ does not add square brackets, indicates the value of value, whether it is the first bit in test, or returns TRUE, otherwise false"
Example:
Res=/^k/.test (' ikkk '); Returns false because the value of K is not the first bit.

"/value$/:$ matches any string that ends with value"
Example:
Res=/i$/i.test (' Hai '); Returns true because the preceding i is the last in test

"/uppercase value$/:$ means matching any string ending with value, and JS is case-sensitive, so it doesn't match"
Example:
Res=/i$/i.test (' Hai '); Returns false because I is uppercase, I is not in test, so returns false, which can be made case insensitive by I

"/value{x}/:{x}" represents a string that matches a sequence of x N, and it is either logical if the preceding value does not match, and the following value matches it as true "
Example:
RES=/O (=w)/.test (' HelloWorld '); Returns true; Indicates that the value after O is equal to W, which is equivalent to/ow/, and then whether the value exists in test.
RES=/O (?! W)/.test (' HelloWorld '); Returns true;?! W, which is equivalent to the value of O if there is no W, returns False if not true,
Since it is or logically judged, the first O is followed by W, but the second o is not followed by W, and all it returns true

"/\D/:\D indicates whether the value in test has a number"
Example:
Res=/\d/.test (' aajkldsfj8 ');//[0-9] Returns True, which returns true as long as there is a number in test.

"/\D/:\D indicates whether the value in test does not exist a number"
Example:
Res=/\d/.test (' SDFKJLLSDFJ ');//[^0-9] Returns True, which returns true as long as there is a number in test.


The EXEC () function: Performs a regular match operation in the target string and returns the result as an array.
Parameter: String specified by


"Find value by exec function"
Res=/is/i.exec (' This is a test ');
return: ["is", index:2, Input: "The is a Test"]
The value to retrieve is IS,INDEX2 refers to the location where it started, and input refers to the entire string that was retrieved because I was added so it is case insensitive
Because it returns an array, you can use Res[0] to receive or display on the page, and if Res[value] is greater than the length of the above array, it returns undefined

"Lastindex Property:"

Definition: When there is a global flag in a regular expression the G,tet () function does not start from the beginning of the string, but from the location specified by the property lastindex
Lookup, the default value of the property is 0, so it is still first searched from the beginning of the string, and when a match is found, the test () function changes the value of the lastindex to within the match
The next index position of the tolerance, and when the test () function is executed two times, it will be looked up from that index to find the next match.
Simply put, the test () executes the first match to the value, and the next time it continues retrieving from the last match.

"String can also use a regular expression through the match () function"
Example:
var str= ' This is a test ';
Res=str.match (/is/i); Retrieving the is,i in STR is not case-insensitive.
return value: ["is", index:2, Input: "The is a Test"]
The value it returns is an array type, and the EXEC () function retrieves the same value.

"Global g"
Example:
var str= ' This is a test ';
Res=str.match (/is/ig); Retrieving the is,i in STR is not case-insensitive, and G represents the global.
return value: (2) [' Is ', ' is ']
(2) Indicates the number of IS, the array form output ["is", "is"]

Basic usage of JS 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.