Example of the replace method of a JavaScript string object (for string replacement or regular expression replacement), javascriptreplace

Source: Internet
Author: User

Example of the replace method of a JavaScript string object (for string replacement or regular expression replacement), javascriptreplace

JavaScript replace Method

The replace method is used to replace some strings with other strings, or replace strings that match the regular expression match, and return the replaced strings. The syntax is as follows:

Copy codeThe Code is as follows:
Str_object.replace (reg_exp/str, replacement)

Parameter description:

Parameters Description
Str_object String (object) to be operated)
Reg_exp/str Required. Regular Expression to be matched/string to be replaced
If reg_exp has a global flag, the replace () method replaces all matched substrings. Otherwise, it only replaces the first matched substring.
Replacement Required. String to be replaced

String replacement instance

The following example demonstrates the string replacement instance of the replace method:
Copy codeThe Code is as follows:
<Script language = "JavaScript">

Var str = "www.example.net ";
Document. write (str. replace ("example", "jb51 "));

</Script>

Run this example and output:
Copy codeThe Code is as follows:
Www.jb51.net

Note: String replacement only replaces the first conforming string (only once). To replace all conforming strings in the string, we recommend that you use a regular expression with the global parameter g, see the following example for details.

Regular Expression string replacement instance

In addition to simple string replacement, the replace method also supports regular expression replacement:
Copy codeThe Code is as follows:
<Script language = "JavaScript">

Var str = "www.example.net is a example domains site of INNA .";
Document. write (str. replace (/example/, "jb51 "));

</Script>

Run this example and output:

Copy codeThe Code is as follows:
Www.jb51.net is a example domains site of INNA.

When the global sign g is added to the regular expression:

Copy codeThe Code is as follows:
<Script language = "JavaScript">

Var str = "www.example.net is a example domains site of INNA .";
Document. write (str. replace (/example/g, "jb51 "));

</Script>

Run this example and output:

Copy codeThe Code is as follows:
Www.jb51.net is a 5 idev domains site of INNA.

Note: If you want to ignore case sensitivity, you can add the I parameter:/example/gi.


How can I replace strings with replace regular expressions in javascript?

Follow these steps to make the changes. Do not forget to adopt them. ^_^
Var str = "abc123def ";
Var r =/\ d {3}/; // note that this sentence must be defined separately and cannot be written in ""; otherwise, JavaScript treats it as a normal string.
Var str2 = str. replace (r ,"? ");
Alert (str2 );

Replace multiple strings in javascript replace. For example, replace all a in a sentence with 1 B and replace all with 2.

Whether it is a single sentence or multiple strings...
This is a replacement method in one sentence:
Var str = ""; // The string you want to replace
Var result = str. replace (/a/g, 1). replace (/B/g, 2); // result is the result after replacement ~
Hope to help you ~
By Billskate

Related Article

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.