Skip to content

Instantly share code, notes, and snippets.

@z007
Last active October 24, 2016 06:25
Show Gist options
  • Save z007/9c1323a6b3b0b44d51d7ba5eb803f379 to your computer and use it in GitHub Desktop.
Save z007/9c1323a6b3b0b44d51d7ba5eb803f379 to your computer and use it in GitHub Desktop.
JavaScript去除字符串两边空格 两边trim
http://www.nowamagic.net/javascript/js_TrimInJavascript.php
<script type="text/javascript">
  function trim(str){ //删除左右两端的空格
  return str.replace(/(^\s*)|(\s*$)/g, "");
  }
  function ltrim(str){ //删除左边的空格
  return str.replace(/(^\s*)/g,"");
  }
  function rtrim(str){ //删除右边的空格
  return str.replace(/(\s*$)/g,"");
  }
 </script>
删除字符串中所有的空格
str.replace(/\s/g,"");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment