SoFunction
Updated on 2025-03-03

Description and application of regular expression functions in JScript

As a powerful tool for text replacement, search and extraction under pattern matching, the application of regular expressions (Regular Expression) has gradually penetrated from the unix platform to network development. As a server-side/client script development language JScript, it is increasingly integrating regular expression applications into it to make up for its lack of text processing capabilities. Here, we take JScript 5.5 version as an example to give an overview of the application of regular expressions in it.
First of all, we need to distinguish between two objects about regular expressions in JScript: the Regular Expression object and the RegExp object.
The former contains only information for a specific regular expression instance, while the latter reflects the characteristics of the recent pattern matching through the properties of a global variable.
The former needs to specify a matching pattern before matching, that is, 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 method; the latter does not need to be created, it is an inherent global object, and the result information of each successful matching operation is saved in the properties of this object.

1. Properties of RegExp object: Reflect the result information of the last successful match

input  : Save the string that executes the matching (the searched target string) (>=IE4)
index  :Save the position of the matching first character*>=IE4)
lastIndex: Save the position of the next character of the matching string (>=IE4)
lastMatch($&): Save the matching string (>=IE5.5)
lastParen($+): Save the content of the last submatch of the matching result (the matching content of the last bracket) (>=IE5.5)
leftContext($`): Save all characters in the target string before matching substring (>=IE5.5)
rightContext($'): Save all characters in the target string after matching the substring (>=IE5.5)
$1 - $9: Save the first 9 sub-matches in the match (i.e. the match result in the first 9 brackets) (>=IE4)

2. Introduction to Regular Expression Objects
Expression object definition
Use regular expression pattern matching in scripts. First, set the matching pattern with the following two methods
(1)rgExp=/pattern*/[flags*]
(2)rgExp=new RegExp("pattern",["flags"])
Notice:
a. The escape character "\" in the latter pattern needs to be represented by "\\" to offset the meaning of the escape character "\" in JS. Otherwise, JS will first explain the characters after "\" as its own escape concept.
There are several identifiers (to JScript 5.5 version)
g: Set the current match to global mode
i: Ignore case detection in matches
m: Multi-line search mode
Expression object properties
(1): Match the position of the character after the result, the same
(2): Regular expression matching pattern of reExp object
Expression object method
(1)(pattern,[flags])
Convert rgExp to internal format to speed up the execution of matches, which is more efficient for a large number of pattern-consistent matches
(2)(str)
The str string is matched and searched according to the matching pattern of rgExp. When the global search mode (g) is set in the rgExp object, the matching search starts from the target string position specified by the lastIndex property of the RegExp object; if no global search is set, search starts from the first character of the target string. If no match occurs, return null.
It should be noted that this method returns the matching result in an array, which has three properties.
input: contains the target string, same
index: The position of the matching substring in the target string, the same
lastIndex: The position of a character after the substring matches, the same
(3)(str)
Returns a boolean value to reflect whether there is a matching pattern in the searched target string str. This method does not change the properties of RegExp
4. Methods related to regular expressions
Mainly refers to the method of applying pattern matching in string objects
(1)(rgExp)
Find matching character items in string stringObj according to the regular expression pattern of rgExp object, and return the result as an array. This array has three attribute values, the same as the array attributes returned by the exec method. If there is no match, return null.
It should be noted that if the rgExp object does not set the global matching pattern, the subscript element of the array 0 is the overall content of the match, and 1~9 contains the characters obtained by the sub-match. If the global pattern is set, the array contains all the overall matches found.
(2)(rgExp, replaceText)
Returns a string, that is, replaces the string in stringObj that matches rgExp pattern with replaceText and returns. It should be noted that stringObj itself does not change due to the replacement operation. If you expect all strings in stringObj that conform to the regular expression pattern are replaced, then the global mode must be set when establishing the regular expression pattern.
(3)(rgExp)
Returns the position of the first matching substring

Symbol noun explanation:
Position: indicates the offset between the substring and the target string's first character
reExp: represents a Regular Expression object instance
stringObj: represents a string object
pattern: Regular expression pattern
flags: pattern identification for matching operations

In actual web program development, we can use regular expressions in a targeted manner to meet our string processing requirements
Four JScript routines using regular expressions are attached below. These examples are mainly used to familiarize yourself with the use of regular expressions.

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((emailStr))
 {
alert("Valid email address!");
  return true;
 }
 else
 {
alert("Invalid email address!");
  return false;
 }
}
</script>

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

3. Pattern search string
<script language='JScript'>
var index, pattern;
var str = "four for fall fell fallen fallsing fall falls waterfalls ";
pattern = /\bfalls\b/i;
index = (pattern);
alert('The position of match is at ' + index);
</script>

3. Regular expression attribute routine
<script language='JScript'>
function matchAttrib()
{
   var s=''; 
   var re = new RegExp("d(b+)(d)","ig");
   var str = "cdbBbdbsbdbdz";
   while((arr = (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 : " +  + "<br>";
    s += "index returns : " +  + "<br>";
    s += "lastIndex returns : " +  + "<br>";
    s += "lastMatch returns: " +  + "<br>";
    s += "leftContext returns: " +  + "<br>";
    s += "rightContext returns: " +  + "<br>"; 
    s += "lastParen returns: " +  + "<br>";
    s += " returns: " +  + "<br>";
    s += " returns: " +  + "<br>";
    s += " returns: " +  + "<br>";
    s += " returns: " +  + "<br>";
    s += " returns: " +  + "<br>";
   }
   return(s);                            //Return results.
}
(matchAttrib());
</script>