V2版本: 改进操作界面,统一表单样式,更多工具支持批量生成,欢迎使用 到达~


文库 阅读
作者: xiaoyu 05/10 20:03:40

JavaScript trim实现(去除字符串左侧右侧指定字符)

// 去除字符串首尾的全部空白

  var str = ' Ruchee ';
  console.log(str.trim(), str.trim().length);
  // "Ruchee"  6
// 去除字符串左侧空白

  str = ' Ruchee ';
  console.log('"' + str.replace(/^\s*/, '') + '"', str.replace(/^\s*/, '').length);
  console.log('"' + str.trimStart() + '"', str.trimStart().length); // ES6
  // "Ruchee "  7
// 去除字符串右侧空白

  str = ' Ruchee ';
  console.log('"' + str.replace(/(\s*$)/g, '') + '"', str.replace(/(\s*$)/g, '').length);
  console.log('"' + str.trimEnd() + '"', str.trimEnd().length); // ES6
  // " Ruchee"  7
  
// 去除字符串左侧指定字符

  str = '爱问去除左右的指定字符帅';
  console.log(str.replace(/爱问(.*)/, "$1"));
  // '去除左右的指定字符帅'
  
// 去除字符串右侧指定字符

  str = '漂亮去除左右的指定字符工具';
  console.log(str.replace(/(.*)工具/, "$1"));
  // '漂亮去除左右的指定字符'
  
首页 喜欢 我的 定制
定制咨询
微信二维码
扫一扫上面的二维码,加我为朋友。
微信扫码周一至周六服务
接定制开发需求