Regular Expression matching, replacement, search, and regular expression matching
The previous article introduced the methods for matching, replacing, searching, and cutting Regular Expressions in Java. Next, I will introduce this article to you.Js Regular Expression matching, search and replacement,For more information, see the following section.
Js Regular Expression matching, search and replacement
RegExp is a RegExp object that represents a regular expression. It is a powerful tool for matching string execution modes. The syntax for directly creating RegExp objects in/pattern/attributes: newRegExp (pattern, attributes); parameter patter...
RegExp object
The RegExp object represents a regular expression, which is a powerful tool for matching string execution modes.
Direct amount syntax
/Pattern/attributes
Syntax for creating a RegExp object:
New RegExp (pattern, attributes );
Parameters
The pattern parameter is a string that specifies the regular expression mode or other regular expressions.
The attributes parameter is an optional string that contains the attributes "g", "I", and "m". It is used to specify global matching, case-sensitive matching, and multi-row matching, respectively. Before ECMAScript standardization, the m attribute is not supported. This parameter must be omitted if pattern is a regular expression rather than a string.
Return Value
A new RegExp object with the specified pattern and flag. If the pattern parameter is a regular expression rather than a string, the RegExp () constructor creates a new RegExp object with the same pattern and flag as the specified RegExp.
If RegExp () is called as a function instead of the new operator, it performs the same behavior as when it is called using the new operator. It only returns pattern when pattern is a regular expression, instead of creating a new RegExp object.
Throw
SyntaxError-If pattern is not a valid regular expression, or attributes contains characters other than "g", "I", and "m", this exception is thrown.
TypeError-If pattern is a RegExp object but the attributes parameter is not omitted, this exception is thrown.
Javascript code
Var string = "hello"; var pattern = "/Good/"; document. write (string. replace (pattern, '<strong> $1 </strong>'); // $1 indicates the matched string.
Javascript code
Var string = ""; var pattern = "//"; document. write (string. match (pattern); // return value type: Array [Array]
Javascript code
Var string = ""; var pattern = "//"; document. write (string. search (pattern); // return value type: Int [array]