Description and application of regular expression functions in JScript

xiaoxiao2021-03-06  99

As a pattern matching, a strong tool for text, search, extract, the application of regular expression has gradually penetrated from the UNIX platform to the network development, as a server-side / client's script development language JScript, is increasing The more the regular expression is applied into it to make up for the shortage of itself on text. Here, we use the JScript5.5 version as an example, the application of the regular expression of the regular expression is an overview. First we need to distinguish two objects about regular expressions in JScript: Regular Expression objects and regexp objects. The former contains only a specific regular expression instance information, while the latter reflects the properties of the most recent pattern through a global variable. The former needs to specify a match mode before matching, that is, an instance of a Regular Expression object, and then pass it to a string method, or pass a string as a parameter to this regular expression instance; and the latter does not need to create It is an inherent global object that is saved in this object each time the successful matching operation result information is saved in the properties of this object.

First, the properties of the regexp object: Reaction the results of the last successful match

INPUT: Save the matching string (Searched target string) (> = IE4) Index: Save matching first character *> = IE4) LastIndex: Save the next character of the matching string (>>>>> = IE4) LastMatch ($ &): Save Matched String (> = IE5.5) LastParen ($ ): Save the contents of the last child match (the last parentheses matching content) (> = IE5. 5) LEFTCONTEXT: Save all characters before the matching substrings in the target string (> = IE5.5) RightContext ($ '): Save all characters after the matching substrings in the target string (> = IE5.5) $ 1 - $ 9: Save the first 9 sub-match in the match (that is, the result of the result in the first 9 brackets) (> = IE4)

Second, the Regular Expression object introduction 1. REGULAR Expression object definition Use the regular expression pattern in the script, first waist setting match mode, the method has the following two (1) RE = / Pattern * / [Flags *] (2) Re = new regexp ("pattern", ["flags"]) Note: a. The escape character "/" in the latter mode needs to be indicated by "//" to cancel the meaning of JS in the JS "/" Otherwise, JS first explains the "/" character as its own escape concept. The Flags ID has the following (to JScript 5.5 version) G: Set the current match to global mode I: Ignore the case detection M: Multi-row search mode 2.Regular Expression object properties (1) RGEXP *. LastIndex: Match the result of a character behind the results, with regexp.SlastIndex (2) reexp.source: Reexp.Source: Reexp object regular expression matching mode 3.Regular Expression object method (1) RGEXP.Compile (Pattern, [Flags]) RGEXP is transformed into internal format to speed up the execution of matching, which is more effective for a large number of patterns (2) RGEXP.EXEC (STR) matches the Str string in the RGEXP mode, which is set in the RGEXP object. Global Search Mode (G), match the lookup starting from the target string position specified from the Regexp object LastIndex property; if there is no global search, start searching from the first character of the target string. Returns NULL if there is no match. It should be noted that this method returns the matching result in an array, which has three attributes INPUT: contains the target string, with the regexp.indexindex: Match the position in the target string, with Regexp. IndexlastIndex: The location of a character behind the sub-string, returning the Boolery value with Regexp.lastIndex (3) RGEXP.TEST (STR) to reflect whether there is a matching mode in the target string STR. This method does not change the properties of Regexp 4. The method associated with the regular expression is mainly referring to the method of the application mode matching in the string object (1) Stringobj * .match (RGEXP) Find string Stringobj according to RGEXP object The matching character items are returned in the array form. The array has three attribute values, the same as the array attribute returned by the exec method. Returns NULL if there is no match. It should be noted that if the RGEXP object does not set a global match mode, the array 0 subscript element is a matching whole content, and 1 to 9 include a sub-matched character. If global mode is set, the array contains all the overall matching items. (2) Stringobj.Replace (RGEXP, ReplaceText) Returns a string, which is replaced with RGEXP mode compliant with RGEXP mode to return. It should be noted that the StringObj itself is not changed by the replacement operation. If all strings that match the regular expression mode in StringObj are replaced, set to global mode when establishing regular expression modes. (3) Stringobj.Search (RGEXP) Returns the first position symbol noun explanation of the first matching sub-string: Location: Indicates the offset of the sub-string and the target string first character Reexp: Represent a regular expression object instance StringObj: Represents a string object Pattern: Regular expression mode Flags: Matching operation mode ID

In actual Web program development, we can have targeted use of regular expressions to achieve our string processing requires four JScript routines that use regular expressions, which are mainly used to familiarize with regular expressions. 1.Email Address Validity Detection