The regular expression is a regular expression. It seems that English is better understood than Chinese, that is, checking expression characters
Not compliant !! Regular expressions have a very powerful and complex object Regexp. In javascript1.2
.
Let's take a look at the introduction of regular expressions:
The regular expression object is used to regulate a standard expression (that is, the expression operator does not meet specific requirements, such as whether it is an email
It has the attributes and methods used to check whether the given string meets the rules.
In addition, the attributes of individual regular expression objects created by using the Regexp constructor have been pre-defined.
You can use static attributes of objects at any time.
Core objects:
Available in Javascript 1.2 and NES 3.0 and later versions.
The tosource method is added in Versions later than JavaScript 1.3.
Create Cube Method:
Text format or Regexp constructor function.
The text format is as follows:
/Pattern/flags:/mode/flag
The constructor function method is as follows:
New Regexp ("pattern" [, "Flags"]) is new Regexp ("Mode" [, "flag"])
Parameters:
Pattern)
Indicates the text of a regular expression.
Flags)
If this option is specified, flags can be one of the following nominal values:
G: Global match (exact match)
I: Ignore case (case insensitive)
GI: both global match and ignore case (matching all possible values, Case Insensitive)
Note: Do not Mark parameters in text format with quotation marks, while parameters in constructors must be marked with quotation marks. So the following
The expression creates the same regular expression:
/AB + C/I
New Regexp ("AB + C", "I ")
Description:
When using constructor, you must use normal strings to avoid rules (add leading characters to strings.
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:
Character
Meaning: For characters, it usually indicates the literal meaning, indicating that the subsequent characters are special characters, not explained.
For example:/B/matches the character 'B'. By adding a backslash (/B/) before B, the character becomes a special character, indicating
Match the dividing line of a word.
Or:
For a few characters, it is generally described as special. It is pointed out that the subsequent 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 */indicates that it matches 0 or multiple A characters.
To match the literal *, add a backslash before a. For example,/a */matches 'A *'.
Character ^
Meaning: The matched characters must be at the frontend.
For example,/^ A/does not match 'A' in "an A,", but matches 'A' in the top of "an '.
Character $
Meaning: similar to ^, it matches the last character.
For example,/t $/does not match 'T' in "Eater", but matches 'T' in "eat '.
Character *
Meaning: match the first character of * 0 or N times.
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 character before the plus sign once or N times. It is equivalent to {1 ,}.
For example,/a +/matches all 'A' in "Candy" and "caaaaaaandy '.
Character?
Meaning: match? The first character is 0 or 1 time.
Example:/e? Le? /Match 'El' in "angel" and 'le' in "angle '.
Character.
Meaning: (decimal point) match all single characters except line breaks.
For example,/. N/matches 'any' and 'on' in "Nay, an apple is on the tree", but does not match 'nay '.
Character (X)
Meaning: Match 'X' and record the matched value.
For example,/(FOO)/matches and records 'foo' in "foo bar '. Matching substrings can be returned by the element [1],..., [N] In the result array.
Returned by Regexp object attributes $1,..., $9.
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: Here N 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: Here N is a positive integer. Match at least n first characters.
For example,/a {2,} does not match 'A' in "Candy", but matches all 'A' in "caandy" and "caaaaaaandy'
Character {n, m}
Meaning: both N and m are positive integers. Match at least n characters at most before M.
For example,/a {}/does not match any character in "cndy", but matches the first two characters in "Candy," 'A', "caandy ,"
'A' and "caaaaaaandy" are the first three 'A'. Note: Even if "caaaaaaandy" contains many 'A', it only matches the first three.
(Aaa ".
Character [xyz]
Meaning: A one-character list that matches any character in the list. You can use a hyphen to indicate a character range.
For example, [ABCD] is the same as [A-C. They match 'B' in "brisket" and 'C' in "ache '.
Character [^ XYZ]
Meaning: A character complement, that is, it matches everything except the listed characters. You can use a hyphen to indicate
Character range.
For example, [^ ABC] is equivalent to [^ A-C]. They first match 'R' in "brisket" and 'H' in "chop '.
Character [B]
Meaning: match a space (do not confuse with B)
Character B
Meaning: match the boundary of a word, such as a space (do not confuse it 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: X is a control character. Matches the control character of a string.
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-number, which is 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 linefeed.
Character R
Meaning: match a carriage return.
Character s
Meaning: match a single white space character, including space, tab, form feed, line feed, equivalent to [fnrtv].
For example,/SW */matches 'bar' in "foo bar '.
Character s
Meaning: match a single character except the white space character, which is equivalent to [^ fnrtv].
For example,/S/W * matches 'foo' in "foo bar '.
Character t
Meaning: match a tab
Character v
Meaning: match a top Tab
Character W
Meaning: match all numbers, letters, and underscores, equivalent to [A-Za-z0-9 _].
For example,/W/matches 'A' in "apple,", "$5.28,", '5' in "3D.", and '3' in '.
Character W
Meaning: match other characters except numbers, letters, and underscores, equivalent to [^ A-Za-z0-9 _].
For example:/W/or/[^ $ A-Za-z0-9 _]/matches '%' in "50% '.
CHARACTER n
Meaning: Here N is a positive integer. Match the N value of the last substring of a regular expression (left parentheses ).
For example:/Apple (,) sorange1/matches 'apple, orange, cherry, Peach. ".
There is a more complete example.
Note: If the number in the left parentheses is smaller than the number specified by N, N removes the octal escape of a row as the description.
Ooctal and xhex
Meaning: ooctal here is an escape value of octal, and xhex is a hexadecimal escape value, which can be
An ASCII code is embedded in a regular expression.
When the expression is checked, the text symbol provides the method to edit the regular expression. Use text symbols to make regular expressions
The formula is constant. For example, if you use text symbols in a loop to construct a regular expression
Compile multiple times.
Regular Expression object constructor, for example, new Regexp ("AB + C"), provides runtime compilation for regular expressions. When you know
When the expression pattern changes, you should use the constructor, or you do not know the pattern of the regular expression.
When the source is obtained, such as when the user inputs. Once you have defined a regular expression, it can be used anywhere,
It can also be changed. You can use the compilation method to compile a new regular expression for reuse.
A separated pre-defined Regexp object can be used in each window; that is, each separated JavaScript thread runs
To obtain the Regexp object. Because each script cannot be interrupted in a thread, this ensures that different scripts do not overwrite the value of the Regexp object.
Predefined static attributes of a Regexp object: input, multiline, lastmatch, lastparen, leftcontext,
Rightcontext, and from $1 to $9. The input and Multiline attributes can be preset. The values of other static attributes are executing individual regular expressions.
After the exec and test methods of the expression object, and set after the string match and replace methods are executed.
Attribute
Note that several attributes of the Regexp object have both long names and short names (such as Perl ). These names all point to the same value. Perl is
One typeProgramming LanguageWhile JavaScript imitates its regular expression.
Attribute $1,..., $9
Obtain matched substrings, if any.
Attribute $ _
Reference Input
Attribute $ *
See multiline
Attribute $ &
Refer to lastmatch
Attribute $ +
Refer to lastparen
Attribute $'
Refer to leftcontext
Attribute $'
Refer to rightcontext
Attribute Constructor
Specify the object prototype Function
Attribute global
Determines whether to test whether the regular expression cannot match all strings or only conflicts with the first one.
Attribute ignorecase
Determines whether to ignore the case sensitivity when trying to match a string
Attribute Input
When the regular expression is matched, it is the opposite string.
Property lastindex
Determine where the next match starts
Attribute lastmatch
Last matched character
Property lastparen
The last parenthesized when the substring is matched, if any.
Attribute leftcontext
The substring before the last match.
Attribute multiline
Whether to search for strings in multiple rows.
Attribute prototype
Allow attributes to be appended to all objects
Attribute rightcontext
The substring after the last match.
Attribute source
Mode text
Method
Compile Method
Compile a regular expression object
Exec Method
Run regular expression matching
Test Method
Test Regular Expression matching
Tosource Method
Returns the text description of an object. You can use this value to create a new object. Ignore object. TOS
Ource method.
Tostring Method
Returns a string that describes the specified object, regardless of the object. tostring object.
Valueof Method
Returns the original value of the specified diagonal. The object. valueof method is not considered.
In addition, this object inherits the watch and unwatch methods of the object.
Example:
Example 1: The following sample script uses the replace method to convert words in a string. In the replaced text, the script uses the Global Regexp
The value of the $1 and $2 attributes of the object. Note: When passed as the second parameter to the replace method, the $ attribute name of the Regexp object
Name.
<Script language = "javascript1.2">
Re =/(W +) S (W + )/;
STR = "John Smith ";
Newstr = Str. Replace (Re, "$2, $1 ");
Document. Write (newstr)
</SCRIPT>
Result: "Smith, John ".
Example 2: In the following sample script, Regexp. input is set by the change event processing handle. In the getinfo function, the exec Method
Use the value of Regexp. input as its parameter. Note that Regexp has a $ attribute preset.
<Script language = "javascript1.2">
Function getinfo (ABC)
{
Re =/(W +) S (D + )/;
Re.exe C (ABC. value );
Window. Alert (Regexp. $1 + ", your age is" + Regexp. $2 );
}
</SCRIPT>
Enter your surname and age, and press Enter.
<Form> <input type = "text" name = "nameage" onchange = "getinfo (this);"> </form>
</Html>
$1,..., $9 attributes
Match a substring enclosed in parentheses, if any.
Is the Regexp attribute
Static, read-only
Available in Javascript 1.2, NES 3.0 and later versions
Description: Because input is a static attribute, it is not an attribute of an individual regular expression object. You can use Regexp. Input to access this
Attribute.
The number of substrings that can be added with parentheses is unrestricted, but the regular expression object can only keep the last nine strings. If you want to access all
The matching string in parentheses. You can use the returned array.
These attributes can be used to replace the strings (output results) with the Regexp. Replace method ). When this method is used
First, consider the Regexp object. The following is an example. When the regular expression does not contain parentheses, the script is interpreted as $ n literal.
. (Here N is a positive integer ).
For example:
The following script uses the replace method to change the position of words in a string. In the replaced text string, the script uses a regular expression.
The value of the $1 and $2 attributes of the Regexp object. Note: When they pass parameters to the replace method, $ attribute's
The name of the Regexp object.
<Script language = "javascript1.2">
Re =/(W +) S (W + )/;
STR = "John Smith ";
Newstr = Str. Replace (Re, "$2, $1 ");
Document. Write (newstr)
</SCRIPT>
The output result is Smith and John.