Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

trim·

Sunny-117 opened this issue · comments

正则写就可以

return str.replace(/(^\s+)|(\s+$)/g,'')//将前空格和后空格替换为空
function myTrim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}
commented
String.prototype.myTrim = function() {
  return this.replace(/(^\s+) | (\s+$)/g, "");
}