Description and application of the positive expression functions of JScript _ Regular expressions

Source: Internet
Author: User
As a powerful tool for text substitution, search and extraction in pattern matching, the application of regular expression (Regular Expression) has been gradually infiltrated into network development from the UNIX platform, as the server-side/client Script development language JScript, More and more regular expression applications are being incorporated into them to make up for the inadequacy of their ability to deal with the text. Here, we use the JScript5.5 version as an example to summarize the application of regular expressions.
First we need to differentiate between two objects in JScript about regular expressions: Regular expression objects and RegExp objects.
The former contains only the information of a particular regular expression instance, and the latter reflects the attributes of the most recent pattern match through the properties of a global variable.
The former needs to specify a matching pattern before matching, that is, to create an instance of a regular expression object, and then pass it to a string method, or pass a string as an argument to the regular expression instance, which does not need to be created, It is an intrinsic global object, and each successful matching operation result information is saved in the object's properties.

Properties of the RegExp object: Reaction to the results of the most recent successful match

Input: Save execution of matching string (target string searched) (>=ie4)
Index: Save matching first character position *>=ie4)
Lastindex: Holds the position of the next character of the matching string (>=ie4)
Lastmatch ($&): Save a matching string (>=ie5.5)
Lastparen ($+): Saves the last child match of the matching result (the last bracket match) (>=ie5.5)
Leftcontext ($ '): Save all characters before matching substrings in the target string (>=ie5.5)
Rightcontext ($ '): Saves all characters after matching substrings in the target string (>=ie5.5)
$ $: Saves the first 9 child matches in the match (that is, the first 9 parentheses) (>=IE4)

Introduction of Regular Expression objects
1.Regular Expression Object Definition
In the script to use regular expression pattern matching, first waist sets the matching pattern, the method has the following two kinds
(1) rgexp=/pattern*/[flags*]
(2) Rgexp=new RegExp ("pattern", ["flags"])
Attention:
A. The escape character in the latter pattern "\" needs to be represented by "\" to counteract the meaning of the "\" of JS relay, otherwise JS first interprets the characters after "\" as its escape concept.
The B.flags logo has the following (up to JScript 5.5 versions)
G: Set current match as global mode
I: Ignore case detection in matches
M: Multi-line search mode
2.Regular Expression Object Properties
(1) Rgexp.lastindex: matches the position of a character after the result, with Regexp.lastindex
(2) The regular expression matching pattern of the RgExp.source:reExp object
3.Regular Expression Object method
(1) rgexp.compile (Pattern,[flags])
Convert rgexp to internal format for faster matching execution, which is more efficient for a large number of pattern-consistent matches
(2) rgexp.exec (str)
Matches the STR string according to the rgexp pattern, and when the Rgexp object has a global search pattern (g), the matching lookup begins at the target string position specified by the RegExp object lastindex attribute, and if no global search is set, Starts the search from the first character of the target string. Returns null if no match occurs.
Notice that the method returns the matching result within an array with three properties
Input: Contains the target string, same regexp.index
Index: The position of the substring matched to the target string, the same regexp.index
Lastindex: The position of a character that matches the substring followed by the Regexp.lastindex
(3) rgexp.test (str)
Returns a Boolean value that reflects whether a matching pattern exists in the str of the target string being looked up. This method does not change the properties of the RegExp
4. Methods related to regular expressions
Mainly refers to the method of applying pattern matching in string objects
(1) stringobj.match (RGEXP)
Finds the matching character entry in the string stringobj based on the regular expression pattern of the Rgexp object, returning the result as an array. The array has three property values, the same as the array property returned by the Exec method. If there is no match, return null.
It is important to note that if the Rgexp object does not have a global match pattern, the array 0 subscript element is the matching overall content, and 1~9 contains the characters that the substring matches. If the global mode is set, the array contains all the overall occurrences of the search.
(2) Stringobj.replace (Rgexp, ReplaceText)
Returns a string that is replaced with a replacetext string that matches the rgexp pattern in stringobj. It should be noted that the stringobj itself is not changed by the substitution operation. If you expect all strings that match the regular expression pattern in the stringobj to be replaced, you set the regular expression pattern to global mode.
(3) Stringobj.search (RGEXP)
Returns the position of the first substring to match

Symbolic noun Interpretation:
Position: Represents the offset of the substring and the first character of the destination string
Reexp: Represents a regular Expression object instance
Stringobj: Represents a String object
Pattern: Regular Expression patterns
Flags: pattern identification for matching operations

In the development of real web programs, we can use regular expressions to achieve our string processing requirements.
The following are four JScript routines that use regular expressions, which are used primarily to familiarize yourself with the use of regular expressions.

1.email Address validity detection
<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 ("Effective email address!");
return true;
}
Else
{
Alert ("Invalid email address!");
return false;
}
}
</script>

2. String substitution operation
<script language= ' JScript ' >
var r, pattern, re;
var s = "The rain in Spain falls mainly the plain falls.";
pattern =/falls/ig;
Re = S.replace (Re, ' falling ');
Alert (' s = ' + S + ' \ n ' + ' re = ' + re ');
</script>

3. Pattern Lookup String
<script language= ' JScript ' >
var index, pattern;
var str = "Four for fall fell fallen fallsing fall Falls";
pattern =/\bfalls\b/i;
index = Str.search (pattern);
Alert (' The position of the match is at ' + index);
</script>

3. Regular Expression Property routines
<script language= ' JScript ' >
function Matchattrib ()
{
var s= ';
var re = new RegExp ("D (b +) (d)", "IG");
var str = "CDBBBDBSBDBDZ";
while ((arr = re.exec (str))!=null)
{
S + + "=======================================<br>";
S + + "$ returns:" + regexp.$1 + "<br>";
S + + "$ returns:" + regexp.$2 + "<br>";
S + + "$ 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.