SoFunction
Updated on 2025-03-08

Regular expression details 3

When an expression is checked, literal symbols provide a way to edit regular expressions. Using literal symbols can keep regular expressions as constants. For example, if you use literal notation in a loop to construct a regular expression, the regular expression does not need to be compiled repeatedly. Regex object constructor, for example, new RegExp("ab+c"), provides runtime compilation of regular expressions. When you know that the pattern of a regular expression will change, you should use the constructor, or you don't know the pattern of the regular expression, but they are obtained from another source, such as when input by the user. Once you have defined the regular expression, the regular expression can be used anywhere and can be changed, and you can use the compilation method to compile a new regular expression for reuse.
A separate predefined RegExp object can be used in each window; that is, each separate Javascript thread runs to obtain its own RegExp object. Because each script is not interruptible in one thread, this ensures that different scripts do not overwrite the value of the RegExp object. The predefined RegExp object contains static properties: input, multiline, lastMatch, lastParen, leftContext, rightContext, and from $1 to $9. The input and multiline properties can be preset. The values ​​of other static properties are executing individual regularities.
After the exec and test methods of the expression object are set after the match and replace methods of the string are executed.
Attributes
Note that several properties of the RegExp object include both long and short names (like Perl). These names all point to the same value. Perl is a programming language, and Javascript mimics its regular expressions.

Attribute $1,…, $9
Get the matching substring, if any

Attribute $_
Reference input

Attribute $*
Reference multiline

Attribute $&
Refer to lastMatch

Attribute $+
Refer to lastParen

Attribute $`
Refer to leftContext

Attribute $'
Reference rightContext

Attribute constructor
Specified to create object prototype letter

Attribute global
Decide whether to test whether a regular expression cannot match all strings, or just conflict with the first one.

Attribute ignoreCase
Whether to ignore case when trying to match a string

Attribute input
When the regular expression is matched, it is the opposite string.

Attribute lastIndex
Decide on the next match from where to start

Attribute lastMatch
The last matching character

Properties lastParen
When the substring matches, the last parenthesized, if any.

Attribute leftContext
Substring before the last match.

Attribute multiline
Whether to search in multiple lines of string.

Prototype
Allows append attributes to all objects

Attribute rightContext
The substring after the last match.

Attribute source
Pattern text

method
compile method
Compile a regular expression object

exec method
Run regular expression matching

Test method
Test regular expression matching

toSource method
Returns an object's literal description of the specified object; you can use this value to create a new object. No method is considered.

toString method
Returns a string description of the specified object, regardless of the object.

valueOf method
Returns the original value of the specified diagonal. No method is considered.


In addition, this object inherits the object's watch and unwatch methods.
example:
Example 1. The following example script uses the replace method to convert words in the string. In the replaced text, the script uses the values ​​of the $1 and $2 properties of the global RegExp object. Note that when passed to the replace method as the second parameter, the name of the $ attribute of the RegExp object
say.
<script LANGUAGE="Javascript1.2"> 
re = /(\w+)\s(\w+)/; 
str = "John Smith"; 
newstr=(re,"$2, $1"); 
(newstr) 
</script> 
Show result: "Smith, John".

Example 2. In the following example script, the Change event processing handle is set. In the getInfo function, the exec method uses the value as its parameter. Note that RegExp presets the $ attribute.


<script LANGUAGE="Javascript1.2"> 
function getInfo(abc) 

re = /(\w+)\s(\d+)/; 
(); 
(RegExp.$1 + ", your age is " + RegExp.$2); 

</script> 

Please enter your last name and age, and press Enter after entering.
<FORM><INPUT TYPE="TEXT" NAME="NameAge" onChange="getInfo(this);"></FORM> 
</HTML> 


$1,…, $9 attribute
Match substrings enclosed in parentheses, if any.
It is the property of RegExp
Static, read-only

Provided in Javascript 1.2, NES 3.0 or above
Description: Because input is a static property, not an attribute of individual regular expression objects. You can access this property using  .

The number of substrings that can be added with parentheses is not limited, but regular expression objects can only retain the last 9 pieces. If you want to access all matching strings in parentheses, you can use the returned array.

These properties can be used in the character string after the method is replaced (output result). When using this method, there is no need to consider the RegExp object in advance. An example is given below. When the regular expression does not contain parentheses, the script is interpreted as the literal meaning of $n
righteous. (N here is a positive integer).


For example:
The following script uses the replace method to exchange the positions of words in the string. In the replaced text string, the script uses the values ​​of the $1 and $2 properties of the RegExp object. Note: When they pass parameters to replace method, the $ attribute is not considered here
The name of the RegExp object.
<script LANGUAGE="Javascript1.2"> 
re = /(\w+)\s(\w+)/; 
str = "John Smith"; 
newstr=(re,"$2, $1"); 
(newstr) 
</script> 
The output results displayed are: Smith, John.
u/meil/archives/2007/