The previous article to introduce Java regular expression matching, replacement, find, cut the method, next, through this article to introduce JS matching, find and replace, the specific content please see below.
JS regular match, find and replace
The RegExp object regexp represents a regular expression, which is a powerful tool for performing pattern matching on strings. Direct measure syntax/pattern/attributes create regexp object syntax: Newregexp (pattern,attributes), parameter parameter patter ...
REGEXP objects
The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.
Direct volume syntax
/pattern/attributes
To create the syntax for a RegExp object:
New REGEXP (pattern, attributes);
Parameters
Parameter pattern is a string that specifies the schema or other regular expression of the regular expression.
The parameter attributes is an optional string that contains the attributes "G", "I" and "M", which are used to specify global matches, case-sensitive matches, and multiline matching. The M attribute is not supported until ECMAScript is standardized. If pattern is a regular expression, rather than a string, you must omit the argument.
return value
A new RegExp object with the specified pattern and flags. If parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.
If you do not use the new operator and REGEXP () as a function call, its behavior is the same as when called with the new operator, except that when pattern is a regular expression, it returns only the formula and no longer creates a new RegExp object.
Thrown
syntaxerror– throws an exception if the pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".
typeerror– if pattern is a RegExp object, but does not omit the attributes argument, throw the exception.
JavaScript code
var string = "Hello";
var pattern = "/good/";
document.write (String.Replace (Pattern, ' <strong>$1</strong> ')); Represents a matching string
JavaScript code
var string = "Hello";
var pattern = "/good/";
document.write (String.match (pattern)); Return value type: array [arrays]
JavaScript code
var string = "Hello";
var pattern = "/good/";
document.write (String.search (pattern)); Return value type: Int [array]