String.prototype.trim

发布时间 2023-03-28 09:48:57作者: yinghualeihenmei

https://www.cnblogs.com/excellencesy/p/7877847.html

https://blog.csdn.net/weixin_30892987/article/details/96134664

/*内置对象添加方法:String.prototype.trim(给String添加一个trim方法)
*^这个是以什么什么开头
*$这个是以什么什么结尾
*‘/s是String /d是数字’
*replace(/^\s+/ , "")把以字符开头的字符串替换为空字符串,
*replace(/\s+$/ , "")把以字符结尾的字符串替换为空字符串,
*/
String.prototype.trim = function()
{
return this.replace(/^\s+/ , "").replace(/\s+$/ , "");
}

 

正则表达式/(^\s*)|(\s*$)/g意思

包含以空格、回车符等字符开头 或者 空格、回车符等字符结尾 的字符串,可过滤出所有空格、回车符的字符