Regular expression constructor: new RegExp("pattern"[,"flags"]);
Parameter description:
pattern - A regular expression text
flags - If it exists, it will be the following value:
g: Global Match
i: Ignore case
gi: The above combination
In the constructor, some special characters need to be changed (add "\" before the special characters). Special characters in regular expressions:
Characters Meaning
\ Change meaning, that is, the characters usually after "\" are not explained in accordance with the original meaning, such as /b/ matches the character "b", when /\b/ is added in front of b, the meaning is:
Match the boundaries of a word.
-or-
Restore the regular expression function characters, such as "*" matching it with the metacharacter before it 0 or more times, /a*/ will match a, aa, aaa, and after adding "\", /a\*/
Will only match "a*".
^ Match an input or the beginning of a line, /^a/ matches "an A" and not "An a"
$ Matches the end of an input or line, /a$/ matches "An a" and not "an A"
* Match the previous metacharacter 0 or more times, /ba*/ will match b,ba,baa,baa
+ Match the previous metacharacter 1 or more times, /ba*/ will match ba,baa,baa
? Match the previous metacharacter 0 or 1 time, /ba*/ will match b,ba
(x) Match x saves x in a variable named $1...$9
x|y Match x or y
{n} Exact match n times
{n,} Match more than n times
{n,m} Match n-m times
[xyz] character set, matching any character (or metacharacter) in this set
[^xyz] Does not match any character in this collection
[\b] Match a backspace character
\b Match the boundary of a word
\B Match a word’s non-boundary
\cX Here, X is a control character, /\cM/ matches Ctrl-M
\d Match a character, /\d/ =/[0-9]/
\D Match a non-word numeric character, /\D/ = /[^0-9]/
\n Match a newline character
\r Match a carriage return
\s Match a whitespace character, including \n,\r,\f,\t,\v, etc.
\S Match a non-whitespace character, equal to /[^\n\f\r\t\v]/
\t Match a tab character
\v Match a heavy straight tab
\w Match a character that can form a word (alphanumeric, which is my free translation, including numbers), including underscores, such as [\w] match "$5.98"
5 in it is equal to [a-zA-Z0-9]
\W Match a character that cannot form a word, such as [\W] matches $ in "$5.98", which is equal to [^a-zA-Z0-9].
Having said so much, let's take a look at some examples of the practical application of regular expressions:
HTML code blocking
function mask_HTMLCode(strInput) {
var myReg = /<(\w+)>/;
return (myReg, "<$1>");
}
E-mail address verification:
function test_email(strEmail) {
var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
if((strEmail)) return true;
return false;
}
Properties and methods of regular expression objects:
The predefined regular expression has the following static properties: input, multiline, lastMatch, lastParen, leftContext,
rightContext and $1 to $9. where input and multiline can be preset. The values of other properties are based on the exec or test method after executing
Different conditions assign different values. Many attributes have two names of long and short (perl style), and these two names point to the same value. (
JavaScript simulates perl's regular expression)
Properties of regular expression objects:
Attribute Meaning
$1...$9 If they exist, they are matching substrings
$_ See input
$* See multiline
$& See lastMatch
$+ See lastParen
$` See leftContext
$''See rightContext
constructor Create a special function prototype for an object
global Whether global
ignoreCase: Whether to ignore case (bool type) when matching
input Matched string
lastIndex The index of the last match
lastParen The substring in the last bracket
leftContext The last time it matches the left substring
multiline Whether to perform multiline matching (bool type)
prototype Allows to attach attributes to objects
rightContext The last time it matches the right substring
source regular expression pattern
lastIndex The index of the last match
Methods of regular expression objects:
Method Meaning
compile Comparison of regular expressions
exec Execute search
test Make a match
toSource Returns the definition of a specific object (literal
representing), whose value can be used to create a new object. The overload method is obtained.
toString Returns the string of a specific object. The overload method is obtained.
valueOf Returns the original value of a specific object. Overload method gets
example:
<script language = "JavaScript">
var myReg = /(w+)s(w+)/;
var str = "John Smith";
var newstr = (myReg, "$2, $1");
(newstr);
</script>
Will output "Smith, John"
Parameter description:
pattern - A regular expression text
flags - If it exists, it will be the following value:
g: Global Match
i: Ignore case
gi: The above combination
In the constructor, some special characters need to be changed (add "\" before the special characters). Special characters in regular expressions:
Characters Meaning
\ Change meaning, that is, the characters usually after "\" are not explained in accordance with the original meaning, such as /b/ matches the character "b", when /\b/ is added in front of b, the meaning is:
Match the boundaries of a word.
-or-
Restore the regular expression function characters, such as "*" matching it with the metacharacter before it 0 or more times, /a*/ will match a, aa, aaa, and after adding "\", /a\*/
Will only match "a*".
^ Match an input or the beginning of a line, /^a/ matches "an A" and not "An a"
$ Matches the end of an input or line, /a$/ matches "An a" and not "an A"
* Match the previous metacharacter 0 or more times, /ba*/ will match b,ba,baa,baa
+ Match the previous metacharacter 1 or more times, /ba*/ will match ba,baa,baa
? Match the previous metacharacter 0 or 1 time, /ba*/ will match b,ba
(x) Match x saves x in a variable named $1...$9
x|y Match x or y
{n} Exact match n times
{n,} Match more than n times
{n,m} Match n-m times
[xyz] character set, matching any character (or metacharacter) in this set
[^xyz] Does not match any character in this collection
[\b] Match a backspace character
\b Match the boundary of a word
\B Match a word’s non-boundary
\cX Here, X is a control character, /\cM/ matches Ctrl-M
\d Match a character, /\d/ =/[0-9]/
\D Match a non-word numeric character, /\D/ = /[^0-9]/
\n Match a newline character
\r Match a carriage return
\s Match a whitespace character, including \n,\r,\f,\t,\v, etc.
\S Match a non-whitespace character, equal to /[^\n\f\r\t\v]/
\t Match a tab character
\v Match a heavy straight tab
\w Match a character that can form a word (alphanumeric, which is my free translation, including numbers), including underscores, such as [\w] match "$5.98"
5 in it is equal to [a-zA-Z0-9]
\W Match a character that cannot form a word, such as [\W] matches $ in "$5.98", which is equal to [^a-zA-Z0-9].
Having said so much, let's take a look at some examples of the practical application of regular expressions:
HTML code blocking
function mask_HTMLCode(strInput) {
var myReg = /<(\w+)>/;
return (myReg, "<$1>");
}
E-mail address verification:
function test_email(strEmail) {
var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
if((strEmail)) return true;
return false;
}
Properties and methods of regular expression objects:
The predefined regular expression has the following static properties: input, multiline, lastMatch, lastParen, leftContext,
rightContext and $1 to $9. where input and multiline can be preset. The values of other properties are based on the exec or test method after executing
Different conditions assign different values. Many attributes have two names of long and short (perl style), and these two names point to the same value. (
JavaScript simulates perl's regular expression)
Properties of regular expression objects:
Attribute Meaning
$1...$9 If they exist, they are matching substrings
$_ See input
$* See multiline
$& See lastMatch
$+ See lastParen
$` See leftContext
$''See rightContext
constructor Create a special function prototype for an object
global Whether global
ignoreCase: Whether to ignore case (bool type) when matching
input Matched string
lastIndex The index of the last match
lastParen The substring in the last bracket
leftContext The last time it matches the left substring
multiline Whether to perform multiline matching (bool type)
prototype Allows to attach attributes to objects
rightContext The last time it matches the right substring
source regular expression pattern
lastIndex The index of the last match
Methods of regular expression objects:
Method Meaning
compile Comparison of regular expressions
exec Execute search
test Make a match
toSource Returns the definition of a specific object (literal
representing), whose value can be used to create a new object. The overload method is obtained.
toString Returns the string of a specific object. The overload method is obtained.
valueOf Returns the original value of a specific object. Overload method gets
example:
<script language = "JavaScript">
var myReg = /(w+)s(w+)/;
var str = "John Smith";
var newstr = (myReg, "$2, $1");
(newstr);
</script>
Will output "Smith, John"