Home Web Front-end JS Tutorial JavaScript string object replace method instance (for string replacement or regular replacement)_Basic knowledge

JavaScript string object replace method instance (for string replacement or regular replacement)_Basic knowledge

May 16, 2016 pm 04:33 PM
javascript String replacement

JavaScript replace method

The

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

Copy code The code is as follows:

str_object.replace(reg_exp/str, replacement)

Parameter description:

Parameters Description
str_object String (object) to be operated on
reg_exp/str Required. Regular expression to match/String to replace
参数 说明
str_object 要操作的字符串(对象)
reg_exp/str 必需。要匹配的正则表达式/要替换的字符串
如果 reg_exp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。
replacement 必需。要替换的字符串
If reg_exp has the global flag g, then the replace() method will replace all matching substrings. Otherwise, it only replaces the first matching substring.
replacement Required. String to replace

String replacement example

The following example demonstrates a string replacement instance of the replace method:

Copy code The code is as follows:

<script language="JavaScript">

var str = "www.example.net";
document.write( str.replace("example", "jb51") );

</script>

Run this example, output:

Copy code The code is as follows:

www.jb51.net

Note: String replacement only replaces the first string that meets the requirements (replaces only once). If you want to replace all strings that meet the requirements in the string, it is recommended to use a regular expression pattern with the global parameter g, specifically See example below.

Regular expression string replacement example

In addition to simple string replacement, the

replace method also supports regular expression replacement:

Copy code The code is as follows:

<script language="JavaScript">

var str = "www.example.net is an example domains site of INNA.";
document.write( str.replace(/example/, "jb51") );

</script>

Run this example, output:

Copy code The code is as follows:

www.jb51.net is an example domains site of INNA.

When adding the global flag g to the regular expression:

Copy code The code is as follows:

<script language="JavaScript">

var str = "www.example.net is an example domains site of INNA.";
document.write( str.replace(/example/g, "jb51") );

</script>

Run this example, output:

Copy code The code is 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 .

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

See all articles