Home Web Front-end JS Tutorial JavaScript String.replace function parameter example description_Basic knowledge

JavaScript String.replace function parameter example description_Basic knowledge

May 16, 2016 pm 05:32 PM
replace string Function parameters

Email:longsu2010 at yeah dot net
The function signature of the replace function of js String is as follows:
replace(match/* string OR regular expression*/, replacement/* string OR function*/)
The function is to replace the match in the string with replacement and return the replaced string.
If the first parameter is a string, there is nothing to say, but remember that the match (first) function is executed only once when the string is replaced.

So the first parameter is usually a regular expression, for example:
replace(/a/g, "b") // Replace all a's in the string with b.

The second parameter can be a string, which can contain the grouping of the regular expression of the first parameter, for example:
replace(/(a){2,2}/g, "$1b") // Replace all aa in the string with ab.

If the second parameter is a function, what are the parameters of the function? For example:

Copy code The code is as follows:

"bbabc".replace(/(a )(b)/g, function(){
console.log(arguments)
});

The parameters will be:
1 , the characters matched by the entire regular expression.
2. The content matched by the first group, the matched content of the second group... and so on until the last group.
3. The subscript (position) of this match in the source string.
4. Derived from the string
, so the output of the example is

["ab", "a", "b", 2, "bbabc"]

Second The return value of the parameter will be replaced in the source string, because if the js function has no return value, calling the function will result in undefined, so if the second parameter has no return value, undefined will be replaced in the source string.
If the first parameter is a string and the second parameter is a function, then treat it as the first parameter is a regular expression without grouping, so that the parameters of the second parameter can be determined.
There are some inappropriate expressions in the article, such as "replacement into the source string" (the source string only acts as a template and does not really change, the string is an immutable variable), I hope it will not Misleading everyone.
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

The relationship between C++ function parameter passing methods and thread safety The relationship between C++ function parameter passing methods and thread safety Apr 12, 2024 pm 12:09 PM

Function parameter passing methods and thread safety: Value passing: Create a copy of the parameter without affecting the original value, which is usually thread safe. Pass by reference: Passing the address, allowing modification of the original value, usually not thread-safe. Pointer passing: Passing a pointer to an address is similar to passing by reference and is usually not thread-safe. In multi-threaded programs, reference and pointer passing should be used with caution, and measures should be taken to prevent data races.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Detailed explanation of C++ function parameters: implementation methods, advantages and disadvantages of indefinite parameter passing Detailed explanation of C++ function parameters: implementation methods, advantages and disadvantages of indefinite parameter passing Apr 28, 2024 am 09:48 AM

C++ indefinite parameter passing: implemented through the... operator, which accepts any number of additional parameters. The advantages include flexibility, scalability, and simplified code. The disadvantages include performance overhead, debugging difficulties, and type safety. Common practical examples include printf() and std::cout, which use va_list to handle a variable number of parameters.

C++ compilation error: Duplicate definition of function parameters, how to solve it? C++ compilation error: Duplicate definition of function parameters, how to solve it? Aug 22, 2023 pm 12:33 PM

As an efficient programming language, C++ is widely used in various fields because of its reliability. However, in the process of writing code, we often encounter some compilation errors, and repeated definition of function parameters is one of them. This article will detail the reasons and solutions for repeatedly defining function parameters. What is repeatedly defining function parameters? In C++ programming, function parameters refer to variables or expressions that appear in function definitions and declarations and are used to accept actual parameters passed when a function is called. When defining a function's argument list, each argument must be

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

See all articles