各种浏览器兼容trim()的方法
2024-08-31 06:48:53
一、利用while方法解决
function trim(str) {
while (str[0] == ' ') {
str = str.slice(1);
}
while (str[str.length - 1] == " ") {
str = str.slice(0, str.length - 1);
}
return str;
}
二、利用正则方法检测开头和结尾的空格
function qdkg(str) {
// return str.replace(/^\s+/,"").replace(/\s+$/,"");
if (String.prototype.trim) {
return str.trim();
} else {
return str.replace(/^\s+/, "").replace(/\s+$/, "");
}
}
三 检测原型prototype中有没有trim()方法,如果没有则更改prototype
if(!String.prototype.trim){
String.prototype.trim = function () {
return this.replace(/^\s+/, "").replace(/\s+$/, "");
}
}
最新文章
- NSTimer整理总结
- List<;Object>;转换为JSONArray二
- 解决Linux系统下程序找不到动态库的方法
- 读Java Concurrency in Practice. 第六章.
- Nuget~管理自己的包包~丢了的包包快速恢复
- USACO Section 2.2: Party Lamps
- iOS: 学习笔记实例, 用代码控制视图创建与切换
- Thread-safety with regular expressions in Java
- Hadoop2.7.3+Spark2.1.0 完全分布式环境 搭建全过程
- linux系统增加开机启动服务/应用
- iOS开发出错whose view is not in the window hierarchy!的解决
- 大厂们的 redis 集群方案
- input子系统 KeyPad-Touch上报数据格式与机制【转】
- linux服务器上简单命令
- liunx top命令详解
- SQL 查询存储过程
- 【python-opencv】图像直方图
- apache重写规则 rewrite
- Error: Target id is not valid ABIs: no ABIs 解决方法
- Imperva正则表达式的添加以及使用