当前位置:网站首页>js去除字符串空格
js去除字符串空格
2022-06-24 12:59:00 【莉兹Liz】
js原生方法trim()
str = str.trim();
局限:只能去除字符串两边空格
let str = " 1 2 3 ";
str0 = str.trim();
// "1 2 3"
正则表达式
去除字符串所有空格
str = str.replace(/\s*/g,"");
去除字符串两边空格
str = str.replace(/^\s*|\s*$/g,"");
去除字符串左侧空格
str = str.replace(/^\s*/g,"");
去除字符串右侧空格
str = str.replace(/\s*$/g,"");
str1 = str.replace(/\s*/g,"");
// "123"
str2 = str.replace(/^\s*|\s*$/g,"");
// "1 2 3"
str3 = str.replace(/^\s*/g,"");
// "1 2 3 "
str4 = str.replace(/\s*$/g,"");
// " 1 2 3"
参考
边栏推荐
- 2022年烟花爆竹生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作
- Vim 常用快捷键
- Liux command
- npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
- 2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
- Kotlin coroutine context and scheduler
- Prometheus pushgateway
- 华为 PC 逆势增长,产品力决定一切
- Golden age ticket: Web3.0 Security Manual
- Can a team do both projects and products?
猜你喜欢

HarmonyOS.2
![[5g NR] 5g NR system architecture](/img/78/6db70c18887c7a3920843d1cb4bdf3.png)
[5g NR] 5g NR system architecture

吉时利静电计宽测量范围

Vulnerability management mistakes that CIOs still make

The research on the report "market insight into China's database security capabilities, 2022" was officially launched

HarmonyOS-3

AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)

Usage of multimeter

Gatling 性能测试

居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文
随机推荐
国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
Usage of multimeter
kotlin 接口 泛型 协变 逆变
8 - Format integers and floating point numbers
These default routes and static routes can not be configured and deployed. What kind of network workers are they!
Jerry's seamless looping [chapter]
记录一次Mongotemplate的And和Or的各种套
真正的项目经理强者,都是闭环高手!
Talk about GC of JVM
HarmonyOS-3
Redis scenario
Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!
kotlin 异步流
杰理之.睡眠以后定时唤醒系统继续跑不复位【篇】
The project manager needs to look at six characteristics to build a team
2022年施工升降机司机(建筑特殊工种)考试试题及在线模拟考试
The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
详解kubernetes备份恢复利器 Velero | 深入了解Carina系列第三期
#21Set经典案例