Regular expressions match, replace, find _ Regular expressions

Source: Internet
Author: User

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]

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.