SoFunction
Updated on 2025-04-12

Commonly used regular expressions collection of javascript page 2/2



test method
Returns a Boolean value indicating whether a pattern exists in the string being looked up.
(str)
parameter
rgexp
Required option. A regular expression object containing a regular expression pattern or available flags.
str
Required option. The string to be tested on it.
illustrate
The test method checks whether a pattern exists in the string, and returns true if it exists, otherwise returns false.
The properties of the global RegExp object cannot be modified by the test method.
Example
The following example illustrates the usage of the test method:
Copy the codeThe code is as follows:

function TestDemo(re, s)
{
var s1; // Declare variables.
// Check whether the string has regular expressions.
if ((s)) // Test whether it exists.
s1 = " contains "; // s contains the pattern.
else
s1 = " does not contain "; // s does not contain the pattern.
return("'" + s + "'" + s1 + "'"+ + "'"); // Returns the string.
}

Function call: (TestDemo(/ain+/ ,"The rain in Spain falls mainly in the plain."));
Return value: 'The rain in Spain falls mainly in the plain.' contains 'ain+'
match method
Perform a lookup on a string using regular expression pattern and return the result containing the lookup as an array. \\
(rgExp)
parameter

stringObj

Required option. A String object or string literal that searches for it.

rgExp

Required option. A regular expression object containing regular expression patterns and available flags. It can also be a variable name or string literal that contains regular expression patterns and available flags.

illustrate

If the match method does not find a match, return null. If a match is found, returns an array and updates the properties of the global RegExp object to reflect the matching result.

The array returned by the match method has three properties: input, index, and lastIndex. The Input property contains the entire searched string. The Index property contains the location of the substring that matches the entire string being found. The LastIndex property contains the next position of the last character in the last match.

If the global flag (g) is not set, the 0 element of the array contains the entire match, while the 1-n element contains any submatch that has appeared in the match. This is equivalent to an exec method with no global flag set. If a global flag is set, elements 0 to n contain all matches.

Example

The following example demonstrates the usage of the match method:
Copy the codeThe code is as follows:

function MatchDemo()
{
var r, re; // Declare variables.
var s = "The rain in Spain falls mainly in the plain";
re = /ain/i; // Create regular expression pattern.
r = (re); // Try to match the search string.
return(r); // Return to the first time "ain" appears.
}

Return value: ain

This example illustrates the usage of the match method set with the g flag.
Copy the codeThe code is as follows:

function MatchDemo()
{
var r, re; // Declare variables.
var s = "The rain in Spain falls mainly in the plain";
re = /ain/ig; // Create regular expression pattern.
r = (re); // Try to match the search string.
return(r); // The returned array contains all "ain"
// Four matches appearing.
}

Return value: ain,ain,ain,ain

The above lines of code demonstrate the usage of the match method of string literals.

var r, re = "Spain";
r = "The rain in Spain".replace(re, "Canada");
return r;
Return value: The rain in Canada

Search Method

Returns the position of the first substring that matches the regular expression search content.

(rgExp)

parameter

stringObj

Required option. A String object or string literal to look for on it.

rgExp

Required option. A regular expression object containing regular expression patterns and available flags.

illustrate

The search method indicates whether there is a corresponding match. If a match is found, the search method returns an integer value indicating the offset position of the match from the beginning of the string. If no match is found, return -1.

Example

The following example demonstrates the usage of the search method.

function SearchDemo()
{
var r, re; // Declare variables.
var s = "The rain in Spain falls mainly in the plain.";
re = /falls/i; // Create regular expression pattern.
r = (re); // Find strings.
return(r); // Return Boolean result.
}
Return value: 18

Regular expression syntax

A regular expression is a literal pattern composed of ordinary characters (such as characters a to z) and special characters (called metacharacters). This pattern describes one or more strings to be matched when searching for a text body. The regular expression acts as a template to match a character pattern with the searched string.

Here are some examples of regular expressions you might encounter:

JScript. VBScript. Match

/^\[ \t]*$/ "^\[ \t]*$" matches a blank line.

/\d{2}-\d{5}/ "\d{2}-\d{5}" Verify that an ID number consists of a 2-digit number, a hyphen, and a 5-digit number.

/<(.*)>.*<\/\1>/ "<(.*)>.*<\/\1>" matches an HTML tag.

The following table is a complete list of metacharacters and their behavior in the context of regular expressions:

Character Description

\ Mark the next character as a special character, or an primitive character, or a backward reference, or an octal escape character. For example, 'n' matches the character "n". '\n' matches a newline character. The sequence '\\' matches "\" and "\(" matches "(".

^ Matches the start position of the input string. If the Multiline property of the RegExp object is set, ^ also matches

The position after '\n' or '\r'.

$ Matches the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before '\n' or '\r'.

* Matches the previous subexpression zero or multiple times. For example, zo* can match "z" and "zoo". * is equivalent to {0,}.

+ Match the previous subexpression once or more times. For example, 'zo+' can match "zo" and "zoo", but not "z". + is equivalent to {1,}.

? Match the previous subexpression zero or once. For example, "do(es)?" can match "do" in "do" or "does". ? is equivalent to {0,1}.

{n} n is a non-negative integer. Match the n times that are determined. For example, 'o{2}' cannot match 'o' in "Bob", but can match two os in "food".

{n,} n is a non-negative integer. Match at least n times. For example, 'o{2,}' cannot match 'o' in "Bob" but can match all os in "fooooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.

{n,m} m and n are non-negative integers, where n <= m. Match at least n times and match up to m times. Liu, "o{1,3}" will match the first three os in "fooooooood". 'o{0,1}' is equivalent to 'o?'. Please note that there cannot be spaces between commas and two numbers.

? When the character is immediately followed by any other restriction character (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. The non-greedy pattern matches as few strings as possible, while the default greedy pattern matches as many strings as possible. For example, for the string "oooo", 'o+?' will match a single "o", and 'o+' will match all 'o'.

. Match any single character except "\n". To match any characters including '\n', use a pattern like '[.\n]'.

(pattern) Match pattern and get this match. The obtained matches can be obtained from the generated Matches collection, using the SubMatches collection in VBScript. and the $0…$9 attribute in JScript. To match parentheses characters, use '\(' or '\)'.

(?:pattern) Match pattern but does not get the matching result, that is, this is a non-get match and is not stored for future use. This is useful when using the "or" character (|) to combine various parts of a pattern. For example, 'industr(?:y|ies) is a simpler expression than 'industry|industries'.

(?=pattern) Forward pre-check, matching the lookup string at the beginning of any string matching pattern. This is a non-get match, that is, the match does not need to be retrieved for later use. For example, 'Windows (?=95|98|NT|2000)' can match "Windows" in "Windows 2000", but cannot match "Windows" in "Windows 3.1". Pre-checking does not consume characters, that is, after a match occurs, the next match's search begins immediately after the last match, rather than after the characters containing the pre-checking.

(?!pattern) Negative lookahead matches the search string at any point where a string not matching pattern . This is a non-get match, that is, the match does not need to be retrieved for later use. For example, 'Windows (?!95|98|NT|2000)' can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000". Pre-checking does not consume characters, that is, after a match occurs, the next match search begins immediately after the last match, rather than starting x|y matches x or y after the characters containing the pre-checking. For example, 'z|food' can match "z" or "food". '(z|f)ood' matches "zood" or "food".

[xyz] Character collection. Match any character contained. For example, '[abc]' can match 'a' in "plain".

[^xyz] Negative value character set. Match any characters not included. For example, '[^abc]' can match 'p' in "plain".

[a-z] Character range. Match any character in the specified range. For example, '[a-z]' can match any lowercase alphabetical characters in the range 'a' to 'z'.

[^a-z] Negative value character range. Match any arbitrary characters that are not within the specified range. For example, '[^a-z]' can match any arbitrary character that is not in the range of 'a' to 'z'.

\b Match a word boundary, which means the position between the word and space. For example, 'er\b' can match 'er' in "never" but not 'er' in "verb".

\B Match non-word boundaries. 'er\B' can match 'er' in "verb", but cannot match 'er' in "never".

\cx Matches the control characters specified by x. For example, \cM matches a Control-M or carriage return. The value of x must be one of A-Z or a-z. Otherwise, treat c as an original 'c' character.

\d Matches a numeric character. Equivalent to [0-9].

\D Match a non-numeric character. Equivalent to [^0-9].

\f Match a page break. Equivalent to \x0c and \cL.

\n Match a newline character. Equivalent to \x0a and \cJ.

\r Match a carriage return character. Equivalent to \x0d and \cM.

\s Match any whitespace characters, including spaces, tabs, page breaks, etc. Equivalent to [ \f\n\r\t\v].

\S Match any non-whitespace characters. Equivalent to [^ \f\n\r\t\v].

\t Match a tab character. Equivalent to \x09 and \cI.

\v Match a vertical tab character. Equivalent to \x0b and \cK.

\w Match any word character that includes an underscore. Equivalent to '[A-Za-z0-9_]'.

\W Match any non-word character. Equivalent to '[^A-Za-z0-9_]'.

\xn matches n, where n is a hexadecimal escape value. The hexadecimal escape value must be the length of two numbers that are determined. For example, '\x41' matches "A". '\x041' is equivalent to '\x04' & "1". ASCII encoding can be used in regular expressions. .

\num matches num, where num is a positive integer. Reference to the obtained match. For example, '(.)\1' matches two consecutive identical characters.

\n Identifies an octal escape value or a backward reference. If \n has at least n obtained subexpressions before, n is a backward reference. Otherwise, if n is an octal number (0-7), n is an octal escape value.

\nm Identifies an octal escape value or a backward reference. If \nm has at least obtained subexpressions before is preceded by at least nm, nm is a backward reference. If there are at least n retrieves before \nm, n is a backward reference followed by the literal m. If none of the previous conditions are satisfied, if n and m are both octal numbers (0-7), then \nm will match the octal escape value nm.

\nml If n is an octal number (0-3), and both m and l are octal numbers (0-7), the octal escape value nml is matched.

\un matches n, where n is a Unicode character represented by four hexadecimal numbers. For example, \u00A9 matches the copyright symbol (?).

Priority order

After constructing a regular expression, you can evaluate like a mathematical expression, that is, you can evaluate from left to right and in a priority order.

The following table lists the priority order of various regular expression operators from the highest priority to the lowest priority:
Operator Description

Escape symbol

(), (?:), (?=), [] brackets and square brackets

*, +, ?, {n}, {n,}, {n,m} qualifiers

^, $, \anymetacharacter Position and Order

| "OR" operation

Normal characters

Normal characters consist of all those printed and non-printed characters that are not explicitly specified as metacharacters. This includes all uppercase and lowercase alphabet characters, all numbers, all punctuation marks, and some symbols.

The simplest regular expression is a single normal character that matches the character itself in the searched string. For example, the single-character pattern 'A' can match the letter 'A' that appears anywhere in the searched string. Here are some examples of single-character regular expression patterns:

/a/
/7/
/M/

The equivalent VBScript. The single-character regular expression is:

"a"
"7"
"M"

Multiple single characters can be combined together to get a larger expression. For example, the following JScript. regular expression is nothing else, but an expression created by combining single-character expressions 'a', '7' and 'M'.

/a7M/

The equivalent VBScript. expression is:

"a7M"

Please note that there is no connection operator here. All you need to do is put one character behind another character.
Previous page12Read the full text