Regular expression (I)


Regular expression is regular expression. It seems that English is much better than Chinese in understanding. It is to check the expression symbol
Not in accordance with the regulations!! Regular expressions have a very powerful and complex object, regexp, in JavaScript 1 Version 2 in
Available on.
Let’s take a look at the introduction of regular expressions:
Regular expression objects are used to standardize a regular expression (that is, the expression does not meet specific requirements, such as whether it is email or not)
Address format, etc.), which has properties and methods to check whether the given string conforms to the rules.
In addition, the properties of individual regular expression objects created by regexp constructor have pre-defined regular expressions
Object, you can use them at any time.
Core objects:
JavaScript version 1.2 and NES version 3.0 and above are available.
The toSource method has been added in JavaScript versions after 1.3.
Establishment method:
Text format or regexp constructor function.
Text formatting uses the following formats:
/ pattern / flags i.e. / pattern / flag

The constructor function is used as follows:
New regexp (“pattern” [, “flags”]), that is, new regexp (“mode” [, “flag”])

Text representing a regular expression

If specified, flags can be one of the following values:
G: global match
I: ignore case
GI: both global match and ignore case

Note: the parameters in the text format should not be marked with quotation marks, while the parameters of the constructor function should be marked with quotation marks. So the following
Expression establishes the same regular expression:
  new RegExp(“ab+c”, “i”)

When using constructors, the normal string must be used. It is necessary to avoid the rule (adding leading characters to 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:

Meaning: for characters, it is usually expressed in literal meaning, indicating that the next character is a special character without explanation.
For example: / B / matches the character ‘B’. By adding a backslash before B, that is, / B /, the character becomes a special character, indicating
Match the dividing line of a word.
For several characters, the description is usually special, indicating that the next character is 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’s.
To match the literal *, add a backslash before a; For example: / A * / matches’ a * ‘.

Meaning: the matching character must be at the front.
For example: / ^ A / does not match ‘a’ in ‘an a,’ but matches the first ‘a’ in ‘an A.’.

Meaning: similar to ^, match the last character.
For example, / T $/ does not match’t ‘in’ eater ‘, but matches’t’ in ‘eat’.

Meaning: match the characters before * 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 “run”.

Meaning: match the character before the + sign once or N times. Equivalent to {1,}.
For example, / A + / matches’ a ‘and’ caaandy. ‘ All ‘a’ in.

Meaning: match? The preceding character 0 or 1 times.
For example: / E? le?/ Match ‘El’ and ‘angle.’ ‘Le’ in.

Meaning: (decimal point) matches all single characters except line feed.
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 bar.” ‘foo’ in. The matching substring can be replaced by the prime [1],…, in the result array, [n] Return
Back, or by the property $1,… Of the regexp object$ 9 return.

Character x|y
Meaning: match ‘x’ or ‘y’.
For example: / green | red / matches’ green ‘and “red apple.” in “green apple” ‘Red ‘in.

Character {n}
Meaning: here n is a positive integer. Match the first n characters.
For example: / a {2} / does not match ‘a’ in ‘candy,’ but matches all ‘a’ and ‘caaandy.’ The first two in

Characters {n,}
Meaning: here n is a positive integer. Match at least n preceding characters.
For example: / a {2,} does not match ‘a’ in ‘candy’, but matches all ‘a’ and ‘caaandy.’ All ‘a’ in

Character {n, m}
Meaning: here n and m are positive integers. Matches at least N and at most M preceding characters.
For example: / a {1,3} / does not match any characters in “cndy”, but matches the first two characters in “a” and “caandy” in “candy”
‘a’ and the first three ‘a’ in “caaady”. Note: even if there are many ‘a’ in “caaady”, it only matches the first three
A ‘a’ is “AAA”.

Character [XYZ]
Meaning: a character list that matches any character in the list. You can indicate a character range by hyphen.
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 hyphens – to indicate one
Character range.
For example: [^ ABC] and [^ a-c] are equivalent. They first match ‘R’ and “chop.” in “brisket” ‘H’ in.

Character [b]
Meaning: match a space (don’t confuse with B)

Character B
Meaning: match the dividing line of a word, such as a space (don’t confuse with [b])
For example: / BNW / matches’ no ‘in “noonday”, / WYB / matches “possibly yesterday.” ‘ly’ in.

Character B
Meaning: matches the non dividing line of a word
For example: / WBN / matches’ on ‘in “noonday” and / ybw / matches “possibly yesterday.” ‘Ye’ in.

Character CX
Meaning: the X here is a control character. The control character that matches a string.
For example: / cm / matches control-m in a string.

Character D
Meaning: match a number, equivalent to [0-9].
For example: / D / or / [0-9] / match “B2 is the suite number.” ‘2’ in.

Character D
Meaning: matching any non number is equivalent to [^ 0-9].
For example: / D / or / [^ 0-9] / match “B2 is the suite number.” ‘B’ in.

Character f
Meaning: match a form character

Character n
Meaning: match a newline character

Character R
Meaning: match a carriage return

Character s
Meaning: match a single white space character, including space, tab, form feed and line feed, which is equivalent to [fnrtv].
For example: / SW * / matches “foo bar.” ‘bar’ in.

Character s
Meaning: match a single character other than white space, equivalent to [^ fnrtv].
For example: / S / w * matches “foo bar.” ‘foo’ in.

Character t
Meaning: match a tab

Character V
Meaning: match a header tab

Character w
Meaning: match all numbers, letters and underscores, equivalent to [a-za-z0-9#].
“For example,” 5. “In ‘W’, / apple in ‘3D’,” ‘3’ in.

Character w
Meaning: match other characters except numbers, letters and underscores, which is equivalent to [^ a-za-z0-9_].
For example: / w / or / [^ $a-za-z0-9_]/ Match “50%.” ‘%’ in.

Character n
Meaning: here n is a positive integer. Matches the value of n of the last substring of a regular expression (count left parenthesis).

For example: / Apple (,) sorange1 / matches “apple, orange, cherry, peach.” Under ‘apple, orange’ in
There is a more complete example.
Note: if the number in the left parenthesis is smaller than the number specified by N, n takes the octal escape in the next line as the description.

Characters ooctal and xhex
Meaning: ooctal here is an octal escape value, while xhex is a hexadecimal escape value, which is allowed in one
ASCII code is embedded in regular expressions.

Literal symbols provide a way to edit regular expressions when the expression is checked. Regular expression can be realized by using literal symbols
The formula remains constant. For example, if you use literal symbols in a loop to construct a regular expression, the regular expression does not need to be modified
Compile repeatedly.
Regular expression object constructors, such as new regexp (“ab + C”), provide runtime compilation of regular expressions. When you know
When the pattern of the expression will change, you should use the constructor, or you don’t know the pattern of the regular expression, and they are from another
When the source is obtained, such as input by the user. Once you have defined a regular expression, it 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 non interruptible in one thread, this ensures that different scripts will not overwrite
The value of the regexp object.
The predefined regexp object contains static attributes: input, multiline, lastmatch, lastparen, leftcontext,
Rightcontext, and from $1 to $9. The input and multiline attributes can be preset. The values of other static properties are performed in individual regular
After the exec and test methods of the expression object and after the match and replace methods of the string are executed.

Note that several properties of the regexp object have both long and short names (like Perl). These names all point to the same value. Perl is
A programming language, and JavaScript imitates its regular expressions.

Property $1$ nine
Get the matching substring, if any

Reference input

Refer to multiline

Refer to lastmatch

Refer to lastparen

Reference leftcontext

Attribute $’
Refer to rightcontext

Property constructor
Specifies the function used to prototype the object

Attribute Global
Decide whether to test whether the regular expression does not match all strings, or just conflicts with the first one.

Property ignorecase
Determines whether case is ignored when trying to match a string

Attribute input
When the regular expression is matched, it is the opposite string.

Attribute lastindex
Decide where to start the next match

Attribute lastmatch
Last matching character

Attribute lastparen
When the substring matches, the last parent is sized, if any.

Property leftcontext
Substring before the last match.

Attribute multiline
Whether to search in multiple lines of the string.

Property prototype
Allow attributes to be attached to all objects

Property rightcontext
Substring after the last match.

Attribute source
Pattern text

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 and the specified object; You can use this value to create a new object. Object is not considered toS
Source method.

ToString method
Returns a string describing the specified object, regardless of object ToString object.

Valueof method
Returns the original value of the specified diagonal. Object is not considered Method of value.

In addition, this object inherits the watch and unwatch methods of the object

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 Properties of the object. Note that when passed to the replace method as the second parameter, the name of the $attribute of the regexp object
    <SCRIPT LANGUAGE=”JavaScript1.2″>
    re = /(w+)s(w+)/;
    str = “John Smith”;
    newstr=str.replace(re,”$2, $1″);
The result is displayed: “Smith, John”

Example 2. In the following example script, regexp Input is set by the change event handling handle. In the getinfo function, the exec method
Use regexp The value of input is used as its parameter. Note that regexp presets the $attribute.

    <SCRIPT LANGUAGE=”JavaScript1.2″>
    function getInfo(abc)
    re = /(w+)s(d+)/;
    window.alert(RegExp.$1 + “, your age is ” + RegExp.$2);

Please enter your last name and age, and then press enter.
  <FORM><INPUT TYPE=”TEXT” NAME=”NameAge” onChange=”getInfo(this);”></FORM>

    $1, …, $ 9 properties
Matching substrings enclosed in parentheses, if any.
Is the property of regexp
Static, read-only

Available in JavaScript 1.2, nes 3.0 or above
Description: because input is a static attribute, it is not an attribute of an individual regular expression object. You can use regexp Input access the

There is no limit to the number of substrings that can be parenthesized, but regular expression objects can only retain the last nine. If you want to access all
The matching string in parentheses, you can use the returned array.

These properties can be used in regexp The string replaced by the replace method (output result). When using this method, there is no need to pre
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
Righteousness. (where n 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
The value of the $1 and $2 Properties of the regexp object. Note: when they pass parameters to the 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=str.replace(re,”$2, $1″);
The output displayed is: Smith, John.

Recommended Today

MySQL high availability architecture

1. Why do you need high availability? High availability ha (high availability) is to solve single machine failure. There is no service or machine in the world that is absolutely safe and reliable, but we can achieve high availability of the system through certain schemes, so as to reduce the unacceptable risk of destruction of […]