去首尾空格: swq.replace(/^\s*|\s*$/g, ‘’)
<!doctype html>
<html lang="en" data-framework="backbonejs">
<head>
<meta charset="utf-8">
<title>正则去首尾空格</title>
<script src="jquery.min.js"></script>
</head>
<body>
<h4>去首尾空格</h4>
<p>jq玩法:trim()</p>
<p>js玩法:</p>
<p>去首尾空格:replace(/^\s*|\s*$/g, '')</p>
<p>去首空格:replace(/^\s*/g, '')</p>
<p>去尾空格:replace(/\s*$/g, '')</p>
<h4>去首尾引号</h4>
<p>去首尾引号:replace(/^\"|\"$/g, '')</p>
<p>去首尾空格,引号:replace(/^\s*\"|\"\s*$/g, '')</p>
<script>
//去空格
var swq = " s df d as d f ";
console.log(swq);
//jq玩法:
console.log(swq.trim());
//js玩法:
//去首尾空格
console.log(swq.replace(/^\s*|\s*$/g, ''))
//去首空格
console.log(swq.replace(/^\s*/g, ''))
//去尾空格
console.log(swq.replace(/\s*$/g, ''))
//去引号
var swq2 = ' " dfsdf " ';
console.log(swq2)
//去首尾空格
console.log(swq2.replace(/^\s*|\s*$/g, ''))
//去首尾空格,再去引号
console.log(swq2.replace(/^\s*|\s*$/g, '').replace(/^\"|\"$/g, ''))
//去首尾空格,和引号
console.log(swq2.replace(/^\s*\"|\"\s*$/g, ''))
</script>
</body>
</html>
end