Regular Expression rule syntax
What is the role of a regular expression?
It can:
? Test a mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called Data Validity verification;
? Replace text. You can use a regular expression in a document to identify a specific text, and then delete it all or replace it with another text;
? Extract a substring from the string based on the pattern match. It can be used to search for specific text in text or input fields;
The next article provides examples to illustrate how to use the following rules.
Modifier
Modifier |
Description |
I |
Perform case-insensitive matching. |
G |
Perform global match (search for all matches instead of stopping the first match ). |
M |
Perform multi-row matching. |
Square brackets
Square brackets are used to find characters in a certain range:
Expression |
Description |
[Abc] |
Find any character between square brackets. |
[^ Abc] |
Search for any characters that are not in square brackets. |
[0-9] |
Search for any number ranging from 0 to 9. |
[A-z] |
Search for any characters from lowercase to lowercase. |
A-Z |
Search for any characters from uppercase A to uppercase Z. |
[A-z] |
Search for any characters from uppercase A to lowercase z. |
[Adgk] |
Searches for any character in a given set. |
[^ Adgk] |
Searches for any characters outside the given set. |
(Red | blue | green) |
Find any specified options. |
Metacharacters
Metacharacter is a character with special meanings:
Metacharacters |
Description |
. |
Searches for a single character, except for line breaks and line Terminators. |
\ W |
Search for word characters. |
\ W |
Searches for non-word characters. |
\ D |
Search for numbers. |
\ D |
Searches for non-numeric characters. |
\ S |
Search for blank characters. |
\ S |
Searches for non-blank characters. |
\ B |
Match the word boundary. |
\ B |
Match non-word boundary. |
\ 0 |
Search for NUL characters. |
\ N |
Find a line break. |
\ F |
Find a break. |
\ R |
Find the carriage return. |
\ T |
Search for tabs. |
\ V |
Search for vertical tabs. |
\ Xxx |
Search for characters specified by octal xxx. |
\ Xdd |
Search for the characters specified by dd in hexadecimal notation. |
\ Uxxxx |
Find the Unicode characters specified by the hexadecimal number xxxx. |
Quantifiers
Quantifiers |
Description |
N + |
Match any string containing at least one n. |
N * |
Match any string containing zero or more n. |
N? |
Match any string containing zero or one n. |
N {X} |
Matches the string that contains X n sequences. |
N {X, Y} |
Matches the string that contains X or Y n. |
N {X ,} |
Matches strings that contain at least X n sequences. |
N $ |
Match any string ending with n. |
^ N |
Match any string starting with n. |
? = N |
Match any string followed by the specified string n. |
?! N |
Match any string that is not followed by the specified string n. |
RegExp Object Attributes
Attribute |
Description |
FF |
IE |
Global |
Whether the RegExp object has a flag. |
1 |
4 |
IgnoreCase |
Whether the RegExp object has a flag I. |
1 |
4 |
LastIndex |
An integer that indicates the position of the next matched character. |
1 |
4 |
Multiline |
Whether the RegExp object has a flag m. |
1 |
4 |
Source |
The Source Text of the regular expression. |
1 |
4 |
RegExp object Method
Method |
Description |
FF |
IE |
Compile |
Compile a regular expression. |
1 |
4 |
Exec |
Returns the value specified in the string. Return the value found and locate it. |
1 |
4 |
Test |
Returns the value specified in the string. Returns true or false. |
1 |
4 |
Methods for String objects that support regular expressions
Method |
Description |
FF |
IE |
Search |
Returns the value that matches the regular expression. |
1 |
4 |
Match |
Find the matching of one or more regular expressions. |
1 |
4 |
Replace |
Replace the substring that matches the regular expression. |
1 |
4 |
Split |
Splits a string into a string array. |
1 |
4 |