Explain how to use the replace () method in JavaScript, javascriptreplace

Source: Internet
Author: User

Explain how to use the replace () method in JavaScript, javascriptreplace

This method finds a match between strings of a regular expression and replaces a new substring of the matched substring.

The replacement string can contain the following special replacement modes:

Syntax

string.replace(regexp/substr, newSubStr/function[, flags]);

The following is the detailed information about the parameters:

  • Regexp: A RegExp object. Response for matching the replaced parameter #2.
  • Substr: a string that is replaced by newSubStr.
  • NewSubStr: it replaces the substring received from the parameter #1.
  • Function: a function is called to create a substring.
  • Flags: Any combination string containing the regular expression flag: g-global match, I-case-insensitive, and m-matched multiple rows. This parameter is only used if the first parameter is a string.

Return Value:

  • It returns a new string.

Example:

The following example demonstrates how to use the global and case-insensitive flag, allow replacement, and replace 'append' with the string 'oranges'

 
Example:

The following example demonstrates how to convert words in a string:

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.