Post (csdn): description and application of functions In JScript

Source: Internet
Author: User
Tags valid email address

As a powerful tool for text replacement, search, and extraction in pattern matching, the application of the regular expression (Regular Expression) has gradually penetrated into network development from the UNIX platform, JScript, a scripting language for servers and clients, is increasingly integrating regular expression applications to make up for its lack of processing capabilities in the text. Here, we take jscript5.5 as an example to give an overview of the application of the regular expression.
First, we need to distinguish two regular expression objects in JScript: Regular Expression objects and Regexp objects.
The former only contains information about a specific regular expression instance, while the latter reflects the features of recent pattern matching through the attribute of a global variable.
The former must specify the matching mode before matching, that is, to create an instance of the regular expression object, and then pass it to a string method, or pass a string as a parameter to the regular expression instance. The latter does not need to be created. It is an inherent global object, the result of each successful match operation is saved in the attributes of this object.

1. attributes of the Regexp object: The result information of the last successful match.

Input: Save the matched string (target string to be searched) (> = ie4)
Index: the location where the matching first character is saved *> = ie4)
Lastindex: location where the next character of the matched string is saved (> = ie4)
Lastmatch ($ &): Save the matched string (> = ie5.5)
Lastparen ($ +): Save the content of the last sub-match in the matching result (Matching content in the last parenthesis) (> = ie5.5)
Leftcontext ($ '): saves all the characters in the target string that match the substring (> = ie5.5)
Rightcontext ($ '): saves all characters matching the substring in the target string (> = ie5.5)
$1-$9: Save the first nine Child matches (that is, the matching results in the first nine parentheses) (> = ie4)

II. Introduction to regular expression objects
1. Regular Expression object definition
Use the regular expression pattern matching in the script. First, set the matching pattern in the waist. There are two methods:
(1) rgexp =/pattern */[flags *]
(2) rgexp = new Regexp ("pattern", ["Flags"])
Note:
A. in the latter mode, the escape character "/" needs to be expressed with "//" to offset the meaning of the JS intermediate meaning character, otherwise, JS will first interpret the characters after "/" as its own escape concept.
B. The flags have the following Identifiers (to JScript 5.5)
G: set the current matching to the global mode.
I: Ignore the case sensitivity check in matching.
M: multi-row Search Mode
2. Regular Expression Object Attributes
(1) rgexp. lastindex: the position of the next character in the matching result, which is the same as Regexp. lastindex.
(2) rgexp. Source: Regular Expression matching mode of the reexp object
3. Regular Expression object Method
(1) rgexp. Compile (pattern, [flags])
Converts rgexp to an internal format to accelerate matching execution, which is more effective for matching with a large number of consistent patterns.
(2)rgexp.exe C (STR)
Search for the STR string according to the rgexp matching mode. When the rgexp object sets the global search mode (G ), searches start from the position of the target string specified by the lastindex attribute of the Regexp object. If no global search is set, searches start from the first character of the target string. If no matching occurs, null is returned.
It should be noted that this method places the matching result in a Number Group and returns the result. This array has three attributes.
Input: contains the target string, which is the same as Regexp. index.
Index: Position of the matched substring in the target string, which is the same as Regexp. index.
Lastindex: the position of the character next to the matched substring, which is the same as Regexp. lastindex.
(3) rgexp. Test (STR)
Returns a Boolean value to indicate whether the target string 'str' matches the pattern. This method does not change the Regexp attribute.
4. Regular Expression-related methods
It mainly refers to the method for applying pattern matching in string objects.
(1) stringobj. Match (rgexp)
Search for matching character items in the stringobj string based on the regular expression mode of the rgexp object and return the results in an array. The array has three attribute values, which are the same as those returned by the exec method. If no match exists, null is returned.
Note that if the global match mode is not set for the rgexp object, the array 0 subscript element is the overall content of the match, 1 ~ 9 contains the characters produced by the submatch. If the global mode is set, the array contains all the overall matching items found.
(2) stringobj. Replace (rgexp, replacetext)
Returns a string that replaces the string matching the rgexp pattern in stringobj with replacetext. Note that stringobj itself does not change because of the replacement operation. If all strings in stringobj that conform to the regular expression pattern are expected to be replaced, set the regular expression pattern to global pattern when creating the regular expression pattern.
(3) stringobj. Search (rgexp)
Returns the position of the first matched substring.

Symbol Glossary:
Position: The offset between the substring and the first character of the target string.
Reexp: indicates a regular expression object instance.
Stringobj: A String object.
Pattern: regular expression mode
Flags: Pattern ID of the matching operation

In actual web application development, we can use regular expressions to meet our string processing requirements.
The following are four JScript routines using regular expressions. These examples are mainly used to familiarize themselves with the use of regular expressions.

1. Check email address Validity
<Script language = 'jscript'>
Function validateemail (emailstr)
{
VaR Re =/^ [/W.-] + @ ([0-9a-z] [/W-] +/.) + [A-Z] {2, 3} $/I;
// Or var Re = new Regexp ("^ [// W. -] + @ ([0-9a-z] [// w-] + //.) + [A-Z] {2, 3} $ "," I ");
If (Re. Test (emailstr ))
{
Alert ("valid email address! ");
Return true;
}
Else
{
Alert ("invalid email address! ");
Return false;
}
}
</SCRIPT>

2. String replacement
<Script language = 'jscript'>
VaR R, pattern, RE;
VaR S = "The rain in Spain falls mainly in the plain falls .";
Pattern =/falls/ig;
Re = S. Replace (Re, 'falling ');
Alert ('s = '+ S +'/N' +'re = '+ RE );
</SCRIPT>

3. SEARCH strings in Mode
<Script language = 'jscript'>
VaR index, pattern;
VaR STR = "four for fall fell fallen fallsing fall falls Waterfalls ";
Pattern = // bfils/B/I;
Index = Str. Search (pattern );
Alert (The position of match is at '+ index );
</SCRIPT>

3. Regular Expression attribute routines
<Script language = 'jscript'>
Function matchattrib ()
{
VaR S = '';
VaR Re = new Regexp ("d (B +) (D)", "ig ");
VaR STR = "cdbbbdbsbdbdz ";
While (ARR = re.exe C (STR ))! = NULL)
{
S + = "========================================== ===< br> ";
S + = "$1 returns:" + Regexp. $1 + "<br> ";
S + = "$2 returns:" + Regexp. $2 + "<br> ";
S + = "$3 returns:" + Regexp. $3 + "<br> ";
S + = "input returns:" + Regexp. Input + "<br> ";
S + = "Index Returns:" + Regexp. index + "<br> ";
S + = "lastindex returns:" + Regexp. lastindex + "<br> ";
S + = "lastmatch returns:" + Regexp. lastmatch + "<br> ";
S + = "leftcontext returns:" + Regexp. leftcontext + "<br> ";
S + = "rightcontext returns:" + Regexp. rightcontext + "<br> ";
S + = "lastparen returns:" + Regexp. lastparen + "<br> ";
S + = "Arr. Index Returns:" + arr. index + "<br> ";
S + = "Arr. lastindex returns:" + arr. lastindex + "<br> ";
S + = "Arr. Input returns:" + arr. Input + "<br> ";
S + = "re. lastindex returns:" + RE. lastindex + "<br> ";
S + = "re. Source returns:" + RE. Source + "<br> ";
}
Return (s); // return results.
}
Document. Write (matchattrib ());
</SCRIPT>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.