Repost (9CBS): Description and application of regular expression functions in JScript

zhaozj2021-02-16  62

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) RGEXP = / PATTERN * / [Flags *] (2) RGEXP = New Regexp ("Pattern", ["Flags"]) Note: a. The escape character "/" in the latter mode needs to be represented by "//" to cancel the meaning of the 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 position behind a character, the regular expression matching mode of RGEXP.Source: Reexp.source: Reexp.lastIndex (2) RGEXP.Source: ReexP object (1) RGEXP.Compile (Pattern, [Flags]) Transform RGEXP For the internal format to speed up the execution of the match, this is more effective for a large number of modes (2) RGEXP.EXEC (STR) Match the Str string in RGEXP matching mode, when the RGEXP object sets a global search Mode (g), the match lookup starts 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 property of regexp 4. The method associated with the regular expression is mainly to refer to the method of application mode matching in the string object (1) Stringobj.match (RGEXP) Find string Stringobj according to the regular expression pattern of RGEXP objects. Matching characters, returning the results in array. 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