Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js replaceall和replace的区别

作者:烟寂断魂   发布日期:2026-02-20   浏览:94

// 使用 replace 方法
let str = "Hello world, welcome to the world of JavaScript!";
let newStr = str.replace("world", "universe");
console.log(newStr); // 输出: Hello universe, welcome to the universe of JavaScript!

// 解释: replace 只会替换第一个匹配的子字符串,后面的不会被替换。

// 使用 replaceAll 方法
let str2 = "Hello world, welcome to the world of JavaScript!";
let newStr2 = str2.replaceAll("world", "universe");
console.log(newStr2); // 输出: Hello universe, welcome to the universe of universe!

// 解释: replaceAll 会替换所有匹配的子字符串。

上一篇:js replace /g

下一篇:js string replace

大家都在看

js 数组打乱顺序

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站