// 删除字符串中的指定字符或子字符串
// 示例 1: 使用 replace 方法删除指定字符或子字符串
let str = "hello world";
let newStr = str.replace("world", ""); // 删除 "world"
console.log(newStr); // 输出: "hello "
// 示例 2: 使用 split 和 join 方法删除所有指定字符
let str2 = "hello world";
let charToRemove = "o";
let newStr2 = str2.split(charToRemove).join(""); // 删除所有 "o"
console.log(newStr2); // 输出: "hell wrld"
// 示例 3: 使用正则表达式删除所有匹配的字符或子字符串
let str3 = "hello world";
let regex = /o/g; // 匹配所有 "o"
let newStr3 = str3.replace(regex, ""); // 删除所有 "o"
console.log(newStr3); // 输出: "hell wrld"
上一篇:js 删除指定字符串
下一篇:js 去掉最后一个字符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站