JavaScript string Object Replace method instance (for string substitution or regular substitution) _ Basics

Source: Internet
Author: User

JavaScript Replace method

The Replace method is used to replace some strings in strings, or to replace strings that match a regular match, and to return the replaced string. The syntax is as follows:

Copy Code code as follows:

Str_object.replace (REG_EXP/STR, replacement)

Parameter description:

Parameters Description
Str_object The string to manipulate (object)
Reg_exp/str Necessary. The regular expression to match/the string to replace
If Reg_exp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.
Replacement Necessary. The string to replace

String Substitution instance

The following example shows a string substitution instance of the Replace method:

Copy Code code as follows:

<script language= "JavaScript" >

var str = "Www.example.net";
document.write (Str.replace ("Example", "jb51"));

</script>

Run the example and output:

Copy Code code as follows:

Www.jb51.net

Note: string substitution replaces only the first qualifying string (only once), and if you want to replace all strings that match the required string, it is recommended to use a regular expression with global parameter g, as shown in the following example.

The regular expression string replaces an instance

The Replace method supports regular expression substitution in addition to simple string substitution:

Copy Code code 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 the example and output:

Copy Code code as follows:

Www.jb51.net is a example domains site of Inna.

When you add a global flag G to a regular expression:

Copy Code code 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 the example and output:

Copy Code code as follows:

Www.jb51.net is a 5idev domains site of Inna.

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

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.