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

js 删除字符串

作者:索伦之刃   发布日期:2025-06-08   浏览:31

// 删除字符串中的指定字符或子字符串

// 示例 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 去掉最后一个字符

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

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

Laravel 中文站