JavaScript REGEXP Objects

Source: Internet
Author: User

RegExp Object

The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.

Direct volume syntax

/pattern/attributes

Syntax for creating REGEXP objects:

new RegExp(pattern, attributes);

Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.

The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

return value

A new RegExp object that has the specified pattern and flags. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.

Without the new operator, and REGEXP () as a function call, it behaves as if it were called with the new operator, except that when pattern is a regular expression, it returns only pattern and no longer creates a new RegExp object.

Thrown

SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.

Modifier

I perform a match on case insensitive.

G performs a global match (finds all matches rather than stops after the first match is found).

M performs multi-line matching.

Square brackets

Square brackets are used to find characters in a range:

Expression description

[ABC] finds any character between square brackets.

[^ABC] finds any characters that are not in brackets.

[0-9] Find any number from 0 to 9.

[A-z] finds any characters from lowercase a to lower Z.

[A-z] finds any characters from uppercase A to uppercase Z.

[A-z] finds any characters from uppercase A to lowercase z.

[ADGK] finds any character within a given collection.

[^ADGK] finds any character outside the given set.

Metacharacters

Metacharacters (metacharacter) is a character that has a special meaning:

Metacharacters description

. Finds a single character, in addition to line breaks and line terminators.

<script type="text/javascript">var str="That‘s hot!";var patt1=/h.t/g;document.write(str.match(patt1));//返回hat,hot </script>

\w find word characters.

\w Find non-word characters.

\d find numbers.

\d to find non-numeric characters.

Quantifiers

Quantifier description

n+ matches any string that contains at least one n.

n matches any string that contains 0 or more N.

<script type="text/javascript">var str="1, 100 or 1000?"; var patt1= /10*/g;document.write(str.match(patt1));//返回1,100,1000 对 "1" 进行全局搜索,包括其后紧跟的一个或多个 "0"</script>

N? Matches any string that contains 0 or one n.

N{x} matches a string containing a sequence of X N.

N{x,y} matches a string containing a sequence of X or Y N.

<script type="text/javascript">var str="100, 1000 or 10000?";var patt1=/\d{3,4}/g; document.write(str.match(patt1));//返回100,1000,1000 对包含三位或四位数字序列的子串进行全局搜索:</script>

N{x,} matches a string that contains at least X N of a sequence.

n$ matches any string that ends with N.

^n matches any string that begins with N.

=n matches any string immediately following the specified string n.

<script type="text/javascript">var str="Is this all there is";var patt1=/is(?= all)/;document.write(str.match(patt1));//返回is 对其后紧跟 "all" 的 "is" 进行全局搜索:</script>

?! n matches any string that does not follow the specified string n immediately thereafter.

RegExp Object Properties

Property Description

LastIndex An integer that indicates the position of the character at which to begin the next match.

Multiline RegExp whether the object has a flag

The source text of a regular expression for a source.

RegExp Object Methods

Method description

Compile compile the regular expression.

EXEC retrieves the value specified in the string. Returns the found value and determines its location.

Test retrieves the value specified in the string. Returns TRUE or FALSE.

<script type="text/javascript">var str = "Visit W3School";var patt1 = new RegExp("W3School");var result = patt1.test(str);document.write("Result: " + result);//返回Result: true </script>

Methods for String objects that support regular expressions

Method description

Search retrieves the value that matches the regular expression.

Match finds the matching of one or more regular expressions.

Replace replaces the substring that matches the regular expression.

Split splits the string into an array of strings.

RegExp Object

The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.

Direct volume syntax

/pattern/attributes

Syntax for creating REGEXP objects:

new RegExp(pattern, attributes);参数

Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.

The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

return value

A new RegExp object that has the specified pattern and flags. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.

Without the new operator, and REGEXP () as a function call, it behaves as if it were called with the new operator, except that when pattern is a regular expression, it returns only pattern and no longer creates a new RegExp object.

Thrown

SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.

Modifier

I perform a match on case insensitive.

G performs a global match (finds all matches rather than stops after the first match is found).

M performs multi-line matching.

Square brackets

Square brackets are used to find characters in a range:

Expression description

[ABC] finds any character between square brackets.

[^ABC] finds any characters that are not in brackets.

[0-9] Find any number from 0 to 9.

[A-z] finds any characters from lowercase a to lower Z.

[A-z] finds any characters from uppercase A to uppercase Z.

[A-z] finds any characters from uppercase A to lowercase z.

[ADGK] finds any character within a given collection.

[^ADGK] finds any character outside the given set.

Metacharacters

Metacharacters (metacharacter) is a character that has a special meaning:

Metacharacters description

. Finds a single character, in addition to line breaks and line terminators.

<script type="text/javascript">var str="That‘s hot!";var patt1=/h.t/g;document.write(str.match(patt1));//返回hat,hot </script>

\w find word characters.

\w Find non-word characters.

\d find numbers.

\d to find non-numeric characters.

Quantifiers

Quantifier description

n+ matches any string that contains at least one n.

n matches any string that contains 0 or more N.

<script type="text/javascript">var str="1, 100 or 1000?"; var patt1= /10*/g;document.write(str.match(patt1));//返回1,100,1000 对 "1" 进行全局搜索,包括其后紧跟的一个或多个 "0"</script>

N? Matches any string that contains 0 or one n.

N{x} matches a string containing a sequence of X N.

N{x,y} matches a string containing a sequence of X or Y N.

<script type="text/javascript">var str="100, 1000 or 10000?";var patt1=/\d{3,4}/g; document.write(str.match(patt1));//返回100,1000,1000 对包含三位或四位数字序列的子串进行全局搜索:</script>

N{x,} matches a string that contains at least X N of a sequence.

n$ matches any string that ends with N.

^n matches any string that begins with N.

=n matches any string immediately following the specified string n.

<script type="text/javascript">var str="Is this all there is";var patt1=/is(?= all)/;document.write(str.match(patt1));//返回is 对其后紧跟 "all" 的 "is" 进行全局搜索:</script>

?! n matches any string that does not follow the specified string n immediately thereafter.

RegExp Object Properties

Property Description

LastIndex An integer that indicates the position of the character at which to begin the next match.

Multiline RegExp whether the object has a flag

The source text of a regular expression for a source.

RegExp Object Methods

Method description

Compile compile the regular expression.

EXEC retrieves the value specified in the string. Returns the found value and determines its location.

Test retrieves the value specified in the string. Returns TRUE or FALSE.

<script type="text/javascript">var str = "Visit W3School";var patt1 = new RegExp("W3School");var result = patt1.test(str);document.write("Result: " + result);//返回Result: true </script>

Methods for String objects that support regular expressions

Method description

Search retrieves the value that matches the regular expression.

Match finds the matching of one or more regular expressions.

Replace replaces the substring that matches the regular expression.

Split splits the string into an array of strings.

RegExp Object

The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.

Direct volume syntax

/pattern/attributes

Syntax for creating REGEXP objects:

new RegExp(pattern, attributes);参数

Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.

The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

return value

A new RegExp object that has the specified pattern and flags. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.

Without the new operator, and REGEXP () as a function call, it behaves as if it were called with the new operator, except that when pattern is a regular expression, it returns only pattern and no longer creates a new RegExp object.

Thrown

SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.

Modifier

I perform a match on case insensitive.

G performs a global match (finds all matches rather than stops after the first match is found).

M performs multi-line matching.

Square brackets

Square brackets are used to find characters in a range:

Expression description

[ABC] finds any character between square brackets.

[^ABC] finds any characters that are not in brackets.

[0-9] Find any number from 0 to 9.

[A-z] finds any characters from lowercase a to lower Z.

[A-z] finds any characters from uppercase A to uppercase Z.

[A-z] finds any characters from uppercase A to lowercase z.

[ADGK] finds any character within a given collection.

[^ADGK] finds any character outside the given set.

Metacharacters

Metacharacters (metacharacter) is a character that has a special meaning:

Metacharacters description

. Finds a single character, in addition to line breaks and line terminators.

<script type="text/javascript">var str="That‘s hot!";var patt1=/h.t/g;document.write(str.match(patt1));//返回hat,hot </script>

\w find word characters.

\w Find non-word characters.

\d find numbers.

\d to find non-numeric characters.

Quantifiers

Quantifier description

n+ matches any string that contains at least one n.

n matches any string that contains 0 or more N.

<script type="text/javascript">var str="1, 100 or 1000?"; var patt1= /10*/g;document.write(str.match(patt1));//返回1,100,1000 对 "1" 进行全局搜索,包括其后紧跟的一个或多个 "0"</script>

N? Matches any string that contains 0 or one n.

N{x} matches a string containing a sequence of X N.

N{x,y} matches a string containing a sequence of X or Y N.

<script type="text/javascript">var str="100, 1000 or 10000?";var patt1=/\d{3,4}/g; document.write(str.match(patt1));//返回100,1000,1000 对包含三位或四位数字序列的子串进行全局搜索:</script>

N{x,} matches a string that contains at least X N of a sequence.

n$ matches any string that ends with N.

^n matches any string that begins with N.

=n matches any string immediately following the specified string n.

<script type="text/javascript">var str="Is this all there is";var patt1=/is(?= all)/;document.write(str.match(patt1));//返回is 对其后紧跟 "all" 的 "is" 进行全局搜索:</script>

?! n matches any string that does not follow the specified string n immediately thereafter.

RegExp Object Properties

Property Description

LastIndex An integer that indicates the position of the character at which to begin the next match.

Multiline RegExp whether the object has a flag

The source text of a regular expression for a source.

RegExp Object Methods

Method description

Compile compile the regular expression.

EXEC retrieves the value specified in the string. Returns the found value and determines its location.

Test retrieves the value specified in the string. Returns TRUE or FALSE.

<script type="text/javascript">var str = "Visit W3School";var patt1 = new RegExp("W3School");var result = patt1.test(str);document.write("Result: " + result);//返回Result: true </script>

Methods for String objects that support regular expressions

Method description

Search retrieves the value that matches the regular expression.

Match finds the matching of one or more regular expressions.

Replace replaces the substring that matches the regular expression.

Split splits the string into an array of strings.

JavaScript REGEXP Objects

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.