Regular Expression matching, replacement, search, and regular expression matching

Source: Internet
Author: User

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]

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.