// 正则表达式去首尾空格
var qsw = ' s df d as d f ';
console.log(qsw);
// js方法
console.log(qsw.trim(),'去首尾空格');
// 正则表达式方法
//去首尾空格
console.log(qsw.replace(/^\s*|\s*$/g,''),'去首尾空格')
//去首空格
console.log(qsw.replace(/^\s*/g,''),'去首空格')
//去尾空格
console.log(qsw.replace(/\s*$/g,''),'去尾空格')
//去引号
var qsw2 = ' " dfsdf " ';
console.log(qsw2);
//去首尾空格
console.log(qsw2.replace(/^\s*|\s*$/g,''))
//去首尾空格,再去引号
console.log(qsw2.replace(/^\s*|\s*$/g,'').replace(/^\"|\"$/g, ''))
//去首尾空格和引号
console.log(qsw2.replace(/^\s*\"|\"\s*$/g, ''))
利用正则表达式去除字符串首尾空格
最新推荐文章于 2024-11-18 13:48:59 发布