Home > Web Front-end > JS Tutorial > body text

Usage of replaceall() method in js

下次还敢
Release: 2024-05-06 09:54:19
Original
1043 people have browsed it

replaceAll() method is used to replace all substrings matching the specified pattern in a string. Its usage is as follows: The parameter regexp specifies the regular expression to be matched. Parameter replacement specifies the string used to replace the match. This method modifies the original string. Special characters in regular expressions must be escaped. If the regex uses the global flag (g), all matches are replaced. If the replacement parameter is undefined, the matching substring will be deleted.

Usage of replaceall() method in js

Usage of replaceAll() method

replaceAll() method is used to replace all matches specified in a string A substring of the pattern.

Syntax:

<code class="js">string.replaceAll(regexp, replacement)</code>
Copy after login

Parameters:

  • regexp: Regular to match expression.
  • replacement: String used to replace matches.

Return value:

The new string after replacement.

Usage:

  1. Use regular expression matching:

    <code class="js">let str = "Hello, world!";
    let newStr = str.replaceAll(/world/, "JavaScript");
    // newStr = "Hello, JavaScript!"</code>
    Copy after login
  2. Use string matching:

    <code class="js">let str = "JavaScript is fun!";
    let newStr = str.replaceAll("JavaScript", "Python");
    // newStr = "Python is fun!"</code>
    Copy after login
  3. Use function as replacement:

    <code class="js">let str = "The quick brown fox jumps over the lazy dog";
    let newStr = str.replaceAll(/the/g, (match) => match.toUpperCase());
    // newStr = "The QUIck brown fox jumps over the lazy dog"</code>
    Copy after login

Note:

  • replaceAll() method will modify the original string.
  • All special characters in regular expressions must be escaped.
  • If the global flag (g) is used in the regular expression, all matches will be replaced.
  • If the replacement parameter is undefined, the matching substring will be deleted.

Example:

<code class="js">// 替换所有数字为 "X"
let str = "1234567890";
let newStr = str.replaceAll(/[0-9]/g, "X");
// newStr = "XXXXXXXXXX"

// 替换所有元音为大写
let str = "Hello, world!";
let newStr = str.replaceAll(/[aeiou]/gi, (match) => match.toUpperCase());
// newStr = "H3LL0, w0RLD!"</code>
Copy after login

The above is the detailed content of Usage of replaceall() method in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!