Regular expressions are regular expressions. It seems that English is much easier to understand than Chinese, which is to check the expression characters.
Not comply with regulations! ! Regex has a very powerful and complex object RegExp, which is in JavaScript 1.2 version
Provided on.
Let’s take a look at the introduction to regular expressions:
Regular expression objects are used to standardize a standard expression (that is, the expression character does not meet specific requirements, such as whether it is an email.
address format, etc.), it has properties and methods used to check whether the given string complies with the rule.
In addition, the properties of individual regular expression objects you created with the RegExp constructor have already predefined the regular expression
Static properties of objects that you can use at any time.
Core object:
Provided in JavaScript 1.2, NES 3.0 or above.
The toSource method has been added in JavaScript 1.3 and later versions.
Establishment method:
Text format or RegExp constructor function.
Text creation format uses the following format:
/pattern/flags i.e./mode/mark
The constructor function method is used as follows:
new RegExp("pattern"[, "flags"]) means new RegExp("pattern"[,"mark"])
Parameters:
pattern(mode)
Text representing regular expressions
flags(mark)
If this item is specified, flags can be one of the following values:
g: global match (full match)
i: ignore case (ignore case)
gi: both global match and ignore case (match all possible values, and ignore case)
Note: The parameters in text format should not be marked with quotes, while the parameters of the constructor function should be marked with quotes. So the following
Expressions create the same regular expression:
/ab+c/i
new RegExp("ab+c", "i")
describe:
When using constructors, it is necessary to use normal strings to avoid rules (include leading characters in the string).
For example, the following two statements are equivalent:
re = new RegExp("\w+")
re = /w+/
The following provides a complete list and description of special characters that can be used in regular expressions.
Table 1.3: Special characters in regular expressions:
Characters
Meaning: For characters, they usually mean literally, pointing out that the next character is a special character and will not be explained.
For example: /b/ matches the character 'b', by adding a backslash before b, that is, /b/, the character becomes a special character, indicating that
Match the dividing line of a word.
or:
For several characters, the description is usually special, indicating that the following characters are not special, but should be interpreted literally.
For example: * is a special character that matches any character (including 0 characters); for example: /a*/ means matching 0 or more a.
To match the literal *, add a backslash before a; for example: /a*/match 'a*'.
Character^
Meaning: The character that indicates that the matching must be at the front.
For example: /^A/ does not match 'A' in "an A," but matches 'A' in "An A."
Character $
Meaning: Similar to ^, matching the last character.
For example: /t$/ does not match 't' in "eater", but match 't' in "eater".
Character*
Meaning: Match * 0 or n times before the characters.
For example:/bo*/ matches 'boooo' in "A ghost booooed" or 'b' in "A bird warbled", but does not match "A goat g
any character in runted ".
Character+
Meaning: Match the characters before the + sign 1 or n times. Equivalent to {1,}.
For example: /a+/ matches all 'a' in "candy" and "caaaaaaandy."
Characters?
Meaning: Match? 0 or 1 previous character.
For example: /e?le?/matches 'el' in "angel" and 'le' in "angle."
Characters.
Meaning: (decimal point) matches all individual characters except line breaks.
For example: /.n/ matches 'an' and 'on' in "nay, an apple is on the tree", but does not match 'nay'.
Character (x)
Meaning: Match 'x' and record the matching value.
For example: /(foo)/Match and record 'foo' in "foo bar." Matching substrings can be returned by the element [1], ..., [n] in the result array
Return, or returned by the properties $1, ..., $9 of the RegExp object.
Character x|y
Meaning: Match 'x' or 'y'.
For example: /green|red/ matches 'green' in "green apple" and 'red' in "red apple."
Character {n}
Meaning: n here is a positive integer. Match the previous n characters.
For example: /a{2}/ does not match 'a' in "candy," but matches all 'a' in "caandy," and the first two in "caaandy."
'a'。
Character {n,}
Meaning: n here is a positive integer. Match at least n preceding characters.
For example: /a{2,} does not match 'a' in "candy", but matches all 'a' in "caandy" and all 'a' in "caaaaaandy."
Character {n,m}
Meaning: n and m here are both positive integers. Match at least n at most m previous characters.
For example: /a{1,3}/ does not match any character in "cndy", but matches the first two in "candy,"
'a' and the three 'a' in the first three in "caaaaaaandy", note: Even if there are many 'a' in the "caaaaaaandy", it only matches the three in the first three
'a' is "aaa".
Characters [xyz]
Meaning: a character list, matching any character listed. You can point out a character range by hyphen.
For example: [abcd] is the same as [a-c]. They match 'b' in "brisket" and 'c' in "ache".
Characters[^xyz]
Meaning: One-character complement, that is, it matches everything except the listed characters. You can use hyphen - point out a
Character range.
For example: [^abc] and [^a-c] are equivalent, they first match 'r' in "brisket" and 'h' in "chop."
Characters[b]
Meaning: Match a space (not to be confused with b)
Character b
Meaning: Match the dividing line of a word, such as a space (not to be confused with [b])
For example: /bnw/ matches 'no' in "noonday", /wyb/ matches 'ly' in "possibly yesterday."
Character B
Meaning: Match the non-dividing line of a word
For example: /wBn/ matches 'on' in "noonday", /yBw/ matches 'ye' in "possibly yesterday."
Character cX
Meaning: The X here is a control character. Match a string of control characters.
For example: /cM/ matches control-M in a string.
Character d
Meaning: Matching a number is equivalent to [0-9].
For example: /d/ or /[0-9]/ matches '2' in "B2 is the suite number."
Character D
Meaning: Match any non-digit, equivalent to [^0-9].
For example: /D/or /[^0-9]/ matches 'B' in "B2 is the suite number."
Character f
Meaning: Match a form character
Character n
Meaning: Match a newline
Character r
Meaning: Match a carriage return character
Characters
Meaning: Match a single white space character, including spaces, tab, form feed, line feed, equivalent to [fnrtv].
For example: /sw*/ matches 'bar' in "foo bar."
Character S
Meaning: Match a single character other than the white space character, equivalent to [^ fnrtv].
For example: /S/w* matches 'foo' in "foo bar."
Character t
Meaning: Match a tab character
Character v
Meaning: Match a header tab
Character w
Meaning: Match all numbers and letters and underscores, equivalent to [A-Za-z0-9_].
For example: /w/ matches 'a' in "apple," , '5' in "$5.28," and '3' in "3D."
Character W
Meaning: Matching other characters except numbers, letters and underscores is equivalent to [^A-Za-z0-9_].
For example: /W/ or /[^$A-Za-z0-9_]/match '%' in "50%.".
Character n
Meaning: n here is a positive integer. Matches the value of n of the last substring of a regular expression (counting the left bracket).
For example: /apple(,)sorange1/ matches 'apple, orange' in "apple, orange, cherry, peach.", below
There is a more complete example.
Note: If the number in the left parentheses is smaller than the number specified by n, n takes the octal escape of the next row as the description.
Characters ooctal and xhex
Meaning: Here ooctal is an octal escape value, while xhex is a hex escape value, which is allowed in a
Embed ASCII code in regular expressions.
When an expression is checked, literal symbols provide a way to edit regular expressions. Using text symbols can make regular expressions
The expression remains constant. For example, if you use literal notation in a loop to construct a regular expression, the regular expression does not need to be performed
Compile repeatedly.
Regex object constructor, for example, new RegExp("ab+c"), provides runtime compilation of regular expressions. When you know it
Then when the pattern of the expression changes, you should use the constructor, or you don't know the pattern of the regular expression, but they are from another
When the source is obtained, such as when input by the user. Once you have defined the regular expression, the regular expression can be used anywhere.
And it can be changed, you can use the compilation method to compile a new regular expression for reuse.
A separate predefined RegExp object can be used in each window; that is, each separate JavaScript thread runs
Line to get your own RegExp object. Because each script is not interruptible in one thread, this ensures that different scripts will not be overwritten
Cover the value of the RegExp object.
The static properties contained in the predefined RegExp object: input, multiline, lastMatch, lastParen, leftContext,
rightContext, and from $1 to $9. The input and multiline properties can be preset. The values of other static properties are executing individual rules.
After the exec and test methods of the expression object are set after the match and replace methods of the string are executed.
Attributes
Note that several properties of the RegExp object include both long and short names (like Perl). These names all point to the same value. Perl is
A programming language, and JavaScript mimics its regular expressions.
Attribute $1, ..., $9
Get the matching substring, if any
Attribute $_
Reference input
Attribute $*
Reference multiline
Attribute $&
Reference lastMatch
Attribute $+
Reference lastParen
Attribute $`
Reference leftContext
Attribute $'
Reference rightContext
Attribute constructor
Specified to create object prototype
Attribute global
Decide whether to test whether the regular expression cannot match all strings, or just conflict with the first one.
Attribute ignoreCase
Decide whether to ignore case when trying to match a string
Attribute input
When the regular expression is matched, it is the opposite string.
Attribute lastIndex
Decide on the next match from where
Attribute lastMatch
The last matching character
Properties lastParen
When the substring matches, the last parenthesized, if any.
attribute leftContext
Substring before the last match.
Attribute multiline
Whether to search in multiple lines of the string.
Prototype
Allows appending attributes to all objects
Attribute rightContext
The substring after the last match.
Attribute source
Pattern text
method
compile method
Compile a regular expression object
exec method
Run regular expression matching
test method
Test regular expression matching
toSource method
Returns an object's text description specified object; you can use this value to create a new object. Don't consider it
source method.
toString method
Returns a string to describe the specified object, regardless of the object.
valueOf method
Returns the original value of the specified diagonal. No method is considered.
In addition, this object inherits the object's watch and unwatch methods
example:
Example 1. The following example script uses the replace method to convert words in the string. In the replaced text, the script uses global RegExp
The value of the $1 and $2 attributes of the object. Note that when passed as a second parameter to replace method, the name of the $ attribute of the RegExp object
say.
<SCRIPT LANGUAGE="JavaScript1.2">
re = /(w+)s(w+)/;
str = "John Smith";
newstr=(re,"$2, $1");
(newstr)
</SCRIPT>
Show result: "Smith, John".
Example 2. In the following example script, the Change event processing handle is set. In the getInfo function, the exec method
Use the value as its parameter, note that RegExp presets the $ property.
<SCRIPT LANGUAGE="JavaScript1.2">
function getInfo(abc)
{
re = /(w+)s(d+)/;
();
(RegExp.$1 + ", your age is " + RegExp.$2);
}
</SCRIPT>
Please enter your last name and age, and press Enter after entering.
<FORM><INPUT TYPE="TEXT" NAME="NameAge" onChange="getInfo(this);"></FORM>
</HTML>
$1, ..., $9 attributes
Match substrings enclosed in parentheses, if any.
is the property of RegExp
Static, read only
Provided in JavaScript 1.2, NES 3.0 or above
Description: Because input is a static property, not a property of individual regular expression objects. You can access this using
property.
The number of substrings that can be added with parentheses is not limited, but the regular expression object can only retain the last 9 pieces. If you want to access all
The matching string in parentheses, you can use the returned array.
These properties can be used in the character string after the method is replaced (output result). When using this method, no pre-order
Consider the RegExp object first. An example is given below. When the regular expression does not contain parentheses, the script is interpreted as the literal meaning of $n
righteous. (N here is a positive integer).
For example:
The following script uses the replace method to exchange the positions of words in the string. In the replaced text string, the script uses regular expressions
Values of the $1 and $2 properties of the RegExp object. Note: When they pass parameters to replace method, the $ attribute is not considered here
The name of the RegExp object.
<SCRIPT LANGUAGE="JavaScript1.2">
re = /(w+)s(w+)/;
str = "John Smith";
newstr=(re,"$2, $1");
(newstr)
</SCRIPT>
The output results displayed are: Smith, John.
Not comply with regulations! ! Regex has a very powerful and complex object RegExp, which is in JavaScript 1.2 version
Provided on.
Let’s take a look at the introduction to regular expressions:
Regular expression objects are used to standardize a standard expression (that is, the expression character does not meet specific requirements, such as whether it is an email.
address format, etc.), it has properties and methods used to check whether the given string complies with the rule.
In addition, the properties of individual regular expression objects you created with the RegExp constructor have already predefined the regular expression
Static properties of objects that you can use at any time.
Core object:
Provided in JavaScript 1.2, NES 3.0 or above.
The toSource method has been added in JavaScript 1.3 and later versions.
Establishment method:
Text format or RegExp constructor function.
Text creation format uses the following format:
/pattern/flags i.e./mode/mark
The constructor function method is used as follows:
new RegExp("pattern"[, "flags"]) means new RegExp("pattern"[,"mark"])
Parameters:
pattern(mode)
Text representing regular expressions
flags(mark)
If this item is specified, flags can be one of the following values:
g: global match (full match)
i: ignore case (ignore case)
gi: both global match and ignore case (match all possible values, and ignore case)
Note: The parameters in text format should not be marked with quotes, while the parameters of the constructor function should be marked with quotes. So the following
Expressions create the same regular expression:
/ab+c/i
new RegExp("ab+c", "i")
describe:
When using constructors, it is necessary to use normal strings to avoid rules (include leading characters in the string).
For example, the following two statements are equivalent:
re = new RegExp("\w+")
re = /w+/
The following provides a complete list and description of special characters that can be used in regular expressions.
Table 1.3: Special characters in regular expressions:
Characters
Meaning: For characters, they usually mean literally, pointing out that the next character is a special character and will not be explained.
For example: /b/ matches the character 'b', by adding a backslash before b, that is, /b/, the character becomes a special character, indicating that
Match the dividing line of a word.
or:
For several characters, the description is usually special, indicating that the following characters are not special, but should be interpreted literally.
For example: * is a special character that matches any character (including 0 characters); for example: /a*/ means matching 0 or more a.
To match the literal *, add a backslash before a; for example: /a*/match 'a*'.
Character^
Meaning: The character that indicates that the matching must be at the front.
For example: /^A/ does not match 'A' in "an A," but matches 'A' in "An A."
Character $
Meaning: Similar to ^, matching the last character.
For example: /t$/ does not match 't' in "eater", but match 't' in "eater".
Character*
Meaning: Match * 0 or n times before the characters.
For example:/bo*/ matches 'boooo' in "A ghost booooed" or 'b' in "A bird warbled", but does not match "A goat g
any character in runted ".
Character+
Meaning: Match the characters before the + sign 1 or n times. Equivalent to {1,}.
For example: /a+/ matches all 'a' in "candy" and "caaaaaaandy."
Characters?
Meaning: Match? 0 or 1 previous character.
For example: /e?le?/matches 'el' in "angel" and 'le' in "angle."
Characters.
Meaning: (decimal point) matches all individual characters except line breaks.
For example: /.n/ matches 'an' and 'on' in "nay, an apple is on the tree", but does not match 'nay'.
Character (x)
Meaning: Match 'x' and record the matching value.
For example: /(foo)/Match and record 'foo' in "foo bar." Matching substrings can be returned by the element [1], ..., [n] in the result array
Return, or returned by the properties $1, ..., $9 of the RegExp object.
Character x|y
Meaning: Match 'x' or 'y'.
For example: /green|red/ matches 'green' in "green apple" and 'red' in "red apple."
Character {n}
Meaning: n here is a positive integer. Match the previous n characters.
For example: /a{2}/ does not match 'a' in "candy," but matches all 'a' in "caandy," and the first two in "caaandy."
'a'。
Character {n,}
Meaning: n here is a positive integer. Match at least n preceding characters.
For example: /a{2,} does not match 'a' in "candy", but matches all 'a' in "caandy" and all 'a' in "caaaaaandy."
Character {n,m}
Meaning: n and m here are both positive integers. Match at least n at most m previous characters.
For example: /a{1,3}/ does not match any character in "cndy", but matches the first two in "candy,"
'a' and the three 'a' in the first three in "caaaaaaandy", note: Even if there are many 'a' in the "caaaaaaandy", it only matches the three in the first three
'a' is "aaa".
Characters [xyz]
Meaning: a character list, matching any character listed. You can point out a character range by hyphen.
For example: [abcd] is the same as [a-c]. They match 'b' in "brisket" and 'c' in "ache".
Characters[^xyz]
Meaning: One-character complement, that is, it matches everything except the listed characters. You can use hyphen - point out a
Character range.
For example: [^abc] and [^a-c] are equivalent, they first match 'r' in "brisket" and 'h' in "chop."
Characters[b]
Meaning: Match a space (not to be confused with b)
Character b
Meaning: Match the dividing line of a word, such as a space (not to be confused with [b])
For example: /bnw/ matches 'no' in "noonday", /wyb/ matches 'ly' in "possibly yesterday."
Character B
Meaning: Match the non-dividing line of a word
For example: /wBn/ matches 'on' in "noonday", /yBw/ matches 'ye' in "possibly yesterday."
Character cX
Meaning: The X here is a control character. Match a string of control characters.
For example: /cM/ matches control-M in a string.
Character d
Meaning: Matching a number is equivalent to [0-9].
For example: /d/ or /[0-9]/ matches '2' in "B2 is the suite number."
Character D
Meaning: Match any non-digit, equivalent to [^0-9].
For example: /D/or /[^0-9]/ matches 'B' in "B2 is the suite number."
Character f
Meaning: Match a form character
Character n
Meaning: Match a newline
Character r
Meaning: Match a carriage return character
Characters
Meaning: Match a single white space character, including spaces, tab, form feed, line feed, equivalent to [fnrtv].
For example: /sw*/ matches 'bar' in "foo bar."
Character S
Meaning: Match a single character other than the white space character, equivalent to [^ fnrtv].
For example: /S/w* matches 'foo' in "foo bar."
Character t
Meaning: Match a tab character
Character v
Meaning: Match a header tab
Character w
Meaning: Match all numbers and letters and underscores, equivalent to [A-Za-z0-9_].
For example: /w/ matches 'a' in "apple," , '5' in "$5.28," and '3' in "3D."
Character W
Meaning: Matching other characters except numbers, letters and underscores is equivalent to [^A-Za-z0-9_].
For example: /W/ or /[^$A-Za-z0-9_]/match '%' in "50%.".
Character n
Meaning: n here is a positive integer. Matches the value of n of the last substring of a regular expression (counting the left bracket).
For example: /apple(,)sorange1/ matches 'apple, orange' in "apple, orange, cherry, peach.", below
There is a more complete example.
Note: If the number in the left parentheses is smaller than the number specified by n, n takes the octal escape of the next row as the description.
Characters ooctal and xhex
Meaning: Here ooctal is an octal escape value, while xhex is a hex escape value, which is allowed in a
Embed ASCII code in regular expressions.
When an expression is checked, literal symbols provide a way to edit regular expressions. Using text symbols can make regular expressions
The expression remains constant. For example, if you use literal notation in a loop to construct a regular expression, the regular expression does not need to be performed
Compile repeatedly.
Regex object constructor, for example, new RegExp("ab+c"), provides runtime compilation of regular expressions. When you know it
Then when the pattern of the expression changes, you should use the constructor, or you don't know the pattern of the regular expression, but they are from another
When the source is obtained, such as when input by the user. Once you have defined the regular expression, the regular expression can be used anywhere.
And it can be changed, you can use the compilation method to compile a new regular expression for reuse.
A separate predefined RegExp object can be used in each window; that is, each separate JavaScript thread runs
Line to get your own RegExp object. Because each script is not interruptible in one thread, this ensures that different scripts will not be overwritten
Cover the value of the RegExp object.
The static properties contained in the predefined RegExp object: input, multiline, lastMatch, lastParen, leftContext,
rightContext, and from $1 to $9. The input and multiline properties can be preset. The values of other static properties are executing individual rules.
After the exec and test methods of the expression object are set after the match and replace methods of the string are executed.
Attributes
Note that several properties of the RegExp object include both long and short names (like Perl). These names all point to the same value. Perl is
A programming language, and JavaScript mimics its regular expressions.
Attribute $1, ..., $9
Get the matching substring, if any
Attribute $_
Reference input
Attribute $*
Reference multiline
Attribute $&
Reference lastMatch
Attribute $+
Reference lastParen
Attribute $`
Reference leftContext
Attribute $'
Reference rightContext
Attribute constructor
Specified to create object prototype
Attribute global
Decide whether to test whether the regular expression cannot match all strings, or just conflict with the first one.
Attribute ignoreCase
Decide whether to ignore case when trying to match a string
Attribute input
When the regular expression is matched, it is the opposite string.
Attribute lastIndex
Decide on the next match from where
Attribute lastMatch
The last matching character
Properties lastParen
When the substring matches, the last parenthesized, if any.
attribute leftContext
Substring before the last match.
Attribute multiline
Whether to search in multiple lines of the string.
Prototype
Allows appending attributes to all objects
Attribute rightContext
The substring after the last match.
Attribute source
Pattern text
method
compile method
Compile a regular expression object
exec method
Run regular expression matching
test method
Test regular expression matching
toSource method
Returns an object's text description specified object; you can use this value to create a new object. Don't consider it
source method.
toString method
Returns a string to describe the specified object, regardless of the object.
valueOf method
Returns the original value of the specified diagonal. No method is considered.
In addition, this object inherits the object's watch and unwatch methods
example:
Example 1. The following example script uses the replace method to convert words in the string. In the replaced text, the script uses global RegExp
The value of the $1 and $2 attributes of the object. Note that when passed as a second parameter to replace method, the name of the $ attribute of the RegExp object
say.
<SCRIPT LANGUAGE="JavaScript1.2">
re = /(w+)s(w+)/;
str = "John Smith";
newstr=(re,"$2, $1");
(newstr)
</SCRIPT>
Show result: "Smith, John".
Example 2. In the following example script, the Change event processing handle is set. In the getInfo function, the exec method
Use the value as its parameter, note that RegExp presets the $ property.
<SCRIPT LANGUAGE="JavaScript1.2">
function getInfo(abc)
{
re = /(w+)s(d+)/;
();
(RegExp.$1 + ", your age is " + RegExp.$2);
}
</SCRIPT>
Please enter your last name and age, and press Enter after entering.
<FORM><INPUT TYPE="TEXT" NAME="NameAge" onChange="getInfo(this);"></FORM>
</HTML>
$1, ..., $9 attributes
Match substrings enclosed in parentheses, if any.
is the property of RegExp
Static, read only
Provided in JavaScript 1.2, NES 3.0 or above
Description: Because input is a static property, not a property of individual regular expression objects. You can access this using
property.
The number of substrings that can be added with parentheses is not limited, but the regular expression object can only retain the last 9 pieces. If you want to access all
The matching string in parentheses, you can use the returned array.
These properties can be used in the character string after the method is replaced (output result). When using this method, no pre-order
Consider the RegExp object first. An example is given below. When the regular expression does not contain parentheses, the script is interpreted as the literal meaning of $n
righteous. (N here is a positive integer).
For example:
The following script uses the replace method to exchange the positions of words in the string. In the replaced text string, the script uses regular expressions
Values of the $1 and $2 properties of the RegExp object. Note: When they pass parameters to replace method, the $ attribute is not considered here
The name of the RegExp object.
<SCRIPT LANGUAGE="JavaScript1.2">
re = /(w+)s(w+)/;
str = "John Smith";
newstr=(re,"$2, $1");
(newstr)
</SCRIPT>
The output results displayed are: Smith, John.