Regular expression (II)

Time:2022-4-28

Regular expression specification (II)

The following are not new objects of regular expressions. Please refer to the properties of the corresponding JavaScript object$_ Attribute reference input $* attribute
Reference multiline $& attribute reference lastmatch $+ attribute reference lastparen $` attribute
Reference leftcontext $’attribute reference rightcontext compile method compile regular expression object during script running
Methods belonging to regexp # provide # syntax in JavaScript 1.2 and NES 3.0 and above:
    regexp. Compile (pattern [, flags]) in number: regexp) the name of the regular expression, which can be variable name or text string.
Pattern is the definition text of the regular expression. Flags, if specified, can be one of the following: “g”: match all possible strings
“I”: ignore case “GI”: match all possible strings and ignore case description:
Use the compile method to compile a regular expression created with the regexp constructor function. such
It forces the regular expression to be compiled only once, instead of once every time the regular expression is encountered. When you confirm that regular expressions can
When it remains unchanged, use the compile method to compile it (after obtaining its matching pattern), so that it can be used repeatedly in the script.
You can also use the compile method to change regular expressions during runtime. For example, if the regular expression changes,
You can use the compile method to recompile the object to improve efficiency.
Using this method will change the values of the source, global and ignorecasesource attributes of the regular expression.     constructor
Point out the function of establishing object prototype. Note that the value of this attribute is provided by the function itself, not a string containing the name of regexp Property provided.
In JavaScript 1.1, nes 2.0 and above, ECMA version ecma-262 is provided. Description: refer to object constructor.
The exec method {runs a match search on the specified string. Returns an array of results. It’s regexp’s method
In JavaScript 1.2, nes version 3.0 and above provides the following syntax: regexp exec([str])regexp([str])
Parameter: regexp, the name of the regular expression, which can be a variable name or a literal definition string.
STR, the string to match the regular expression. If omitted, regexp Value of input.
Description: as in the syntax description, the exec method of the regular expression worker can be called directly (using regexp. Exec (STR)) or indirectly (using regexp (STR)).
If you just run to find out if it matches, you can use the string search method.
If the match is successful, the exec method returns an array and updates the value of the regular expression object attribute and the predefined regular expression object, regexp. If the match fails, the exec method returns null.
See the following example: < script language = “javascript1.2” > / / match a B followed by one or more D, followed by a B
/ / ignore case = Myre = / D (B +) (d) / Ig;     myArray = myRe. exec(“cdbBdbsbz”);
< / script > the following is the return value of the script: object attribute / index description example
myArray

Contents of myArray [“dbbd”, “BB”, “d”]
index
Match index 1 based on 0
input
Original string {cdbdbsbz
[0]
Last matched character: dbbd
[1], …[n]
The matching string enclosed in parentheses, if any. The number of parentheses is not limited.   [1] = bB
[2] = d  
myRe
lastIndex
Start the index value of the next matching operation # 5
ignoreCase
Indicates whether “I” is used to ignore case
global
Indicates whether to use the “g” tag to match all possible strings
source
Text string that defines the pattern {D (B +) (d)
RegExp
lastMatch$&
Last matched character: dbbd
leftContext$Q
The latest matches the previous substring C
rightContext$’
Substring after latest match {bsbz
$1, …$9
Matching substrings in parentheses, if any. There is no limit to the number of parentheses, but regexp can only keep the last nine $1 = BB
$2 = d  
lastParen  $+
The last matching substring with parentheses, if any, D

If your regular expression uses the “g” tag, you can use the exec method multiple times to continuously match the same string. When you do
The new match will start from the substring determined by the value of the lastindex attribute of the regular expression. For example, suppose you use the following script:
    <SCRIPT LANGUAGE=”JavaScript1.2″>    myRe=/ab*/g;str = “abbcdefabh”
    myArray = myRe.exec(str);
    document.writeln(“Found “+myArray[0]+”. Next match starts at “+myRe.lastIndex)
    mySecondArray = myRe.exec(str);
    document.writeln(“Found “+mySecondArray[0]+”. Next match starts at “+myRe.lastIndex)
< / script > this script displays the following results: found abb Next match starts at 3
Found ab. next match starts at 9 example:
In the following example, the user enters a name, and the script performs a matching operation based on the input. Then check the array to see if it matches the names of other users.
This script assumes that the last name of the registered user has been saved into array a, perhaps from a database.     <HTML>
    <SCRIPT LANGUAGE=”JavaScript1.2″>    A = [“zhao”,”qian”,”sun”,”li”,”liang”]
    function lookup()     {     firstName = /w+/i();     if (!firstName)
       window. Alert (regexp.input + “illegal input”);      else {       count=0;
For (I = 0; I) enter your last name and press enter.
    <FORM><INPUT TYPE:”TEXT” NAME=”FirstName” onChange=”lookup(this);”></FORM>
< / HTML > whether the “g” tag is used in the regular expression of the global attribute. Regexp property, read only
In JavaScript 1.2, nes version 3.0 and above provide} Description: Global is an attribute of an individual regular expression object
If the “g” tag is used, the value of global is true; Otherwise, it is false. The “g” tag specifies that the regular expression tests for all possible matches.
You can’t change the value of this property directly, but you can call the compile method to change it. Ignorecase # check whether the regular expression uses the “I” tag
Regexp attribute, read-only. Description is provided in JavaScript version 1.2 and NES version 3.0 and above:
Ignorecase is a property of an individual regular expression object.
Returns true if the “I” tag is used, otherwise false. The “I” flag indicates that case is ignored when matching.
You can’t change the value of this attribute directly, but you can change it by calling the compile method. Input indicates the string that the regular expression wants to test$_ Is another name for this property.
The property of regexp is statically {available in JavaScript version 1.2 and NES version 3.0 or above
Description: because input is static, it is not an attribute of an individual regular expression object. You can also use regexp Input.
If no string is provided for the exec or test method of the regular expression, and regexp If there is a value in input, use its value to call the method.
Scripts or browsers can preset the input attribute. If the value is preset and no string is provided when calling exec or test methods
The value of input is used when calling exec or test. Input can be set by the browser in the following way:
When the text form field processing handle is called, input is set to the string of the text input.
When the textarea form field processing handle is called, input is set to the string input in the textarea field. Pay attention to multili
NE is also set to true to match multiple lines of text. When the select form field processing handle is called, input is set to the value of selected text.
When the processing handle of the linked object is called, the input is set to < a href =… > And < / a >.
After the event handler is processed, the value of the input attribute is cleared. Lastindex is a readable / writable integer attribute that indicates where the next match starts.
Regexp attribute} is available in JavaScript 1.2 and NES 3.0 or above
Description: lastindex is an attribute of an individual regular expression object. This property is set only when the “g” tag of the regular expression is used for full string matching. The following rules apply:
If lastindex size is the length of the string, regexp Test and regexp Exec failed and lastindex is set to 0.
If lastindex is equal to the length of the string and the regular expression matches the empty string, the regular expression matches from the position of lastindex.
If lastindex is equal to the length of the string and the regular expression does not match the empty string, the regular expression does not match input and lastindex is set to 0.
Otherwise, lastindex is set to the next point of the last match. For example, execute the script in the following order: Re = / (HI)/ G) match empty string
Re (“Hi”) returns [“Hi”, “Hi”], and lastindex is set to 2
Re (“Hi”) returns [“”], an empty array whose elements with subscript 0 are matching strings. In this case, null is returned
The string is because lastindex is equal to 2 (and still 2), and the length of “Hi” is also 2. Lastmatch last matches the string, $& means the same.
Regexp attribute, static and read-only, is available in JavaScript 1.2 and NES 3.0 or above
Description: because lastmatch is static, it is not an attribute that specifies a regular expression individually. You can also use regexp lastMatch。     lastParen
The matching string with parentheses for the last time, if any$+ It means the same thing. Regexp property, static, read-only
Available in JavaScript 1.2, nes 3.0 or above
Description: because lastparen is static, it is not an attribute of an individual regular expression. You can use regexp Lastparen means the same.
Leftcontext last matches the previous substring, $` has the same meaning. Property of regexp, static, read-only
Available in JavaScript 1.2, nes 3.0 or above
Description: because leftcontext is static and not a regular expression attribute, regexp Leftcontext to express the same meaning.
Multiline reflects whether to match multiple lines of text, $* means the same. Properties of regexp, static
Available in JavaScript 1.2, nes 3.0 or above
Description: because multiline is static rather than the attribute of an individual regular expression, regexp Multiline means the same.
Multiline is true if multiple lines of text are allowed to match, and false if the search must stop on line feed.
Scripts or browsers can set the multiline property. When a textarea’s event handling handle is called, multiline
Set to true. After the event processing handle is processed, the multiline property value is cleared. That is, if you set multili
If ne is true, multiline will be set to false after executing any event processing handle.     prototype
Describe the prototype of the class. You can use prototype to add class properties or methods as required. In order to obtain the capital of prototypes
See function of regexp prototype. Property property. Available from JavaScript 1.1 and NES 2.0
The string on the right of the last match of ecma-262 “rightcontext” in ECMA version, $’has the same effect.
Regexp’s properties are static and read-only} and are available from JavaScript version 1.2 and NES version 3.0 or above
Description: because rightcontext is static and not an attribute of an individual regular expression worker, regexp can be used Rightcontext to achieve the same effect.
Source is a read-only attribute that contains a pattern defined by a regular expression and does not include forward slashes and “g” or “I” tags. Property of regexp, read-only
Available from JavaScript 1.2, nes 3.0 and above
Description: source is an attribute of an individual regular expression object. You can’t change its value directly, but you can change it by calling the compile method.     test
Performs a regular expression matching search for the specified string, returning true or false. Regexp’s method
From JavaScript 1.2, nes 3.0 and above, the} syntax is: regexp test([str])
Parameter: regexp, the name of regular expression, which can be variable name or regular expression definition text string
STR, the string to match. If omitted, regexp The value of input is as a parameter
Description: when you need to know whether a string can match a regular expression, you can use the test method (the same as string. Search)
Method (similar); To get more information (but slower), you can use the exec method (similar to the string. Match method). Example: the following example shows the prompt whether the test is successful:
    function testinput(re, str){
      if (re.test(str))  midstring = ” contains “;
      else midstring = ” does not contain “;
      document.write (str + midstring + re.source);   }    toSource
Return the source code of a string symbolic object regexp method from JavaScript version 1.3 or above syntax: tosource()
Parameter: no} Description: the toSource method returns the following values: for the built-in regexp object, toSource returns the following characters, indicating that the source code is not available:
    function Boolean(){      [native code] }
In the case of regexp, toSource returns a string symbolizing the source code. Usually, this method is automatically called inside JavaScript rather than explicitly called in the code.
For more information, see object ToSource – toString returns the string depicting the specified object. Regexp’s method
Provide ECMA version ecma-262 from JavaScript 1.1 and NES 2.0. Syntax: tostring() parameter: None
Description: regexp object does not consider the toString method of object object; It does not inherit object ToString, for regexp pair
For example, the toString method returns a string representing the object. For example, the following example shows a string representing a regexp object
    myExp = new RegExp(“a+b+c”);    alert(myExp.toString())
Displays “/ A + B + C /” for more, see: object ToString – valueof returns the original value of a regexp object
Regexp’s method # starts from JavaScript version 1.1 # ECMA version: ecma-262 # syntax: valueof()
Parameters: none} Description: the valueof method of regexp returns the original value of regexp object in the form of string, which is the same as regexp ToString is equal.
This method is usually called automatically within JavaScript rather than explicitly. Example: myexp = new regexp (“a + B + C”);
    alert(myExp.valueOf())    displays “/a+b+c/”

Recommended Today

Crash tracking journey of IOS development (I)

Preface: Recently, I encountered a crash blood disaster caused by crash in daily development. In a release in early May, the crash rate of the app developed by the author was directly increased from one thousand to nearly two thousand. At that time, the project leader just needed to report the relevant situation of project […]