ES5-String-trim

346 阅读1分钟

trim方法用于去除字符串两端的空格,返回一个新字符串,不改变原字符串。

'  hello world  '.trim() // "hello world"
'  hello world  '.trimLeft() // "hello world  "
'  hello world  '.trimLeft() // "  hello world"

该方法去除的不仅是空格,还包括制表符(\t\v)、换行符(\n)和回车符(\r)。

'\r\nabc \t'.trim() // 'abc'