正则表达式收藏

去除所有空格:   
str   =   str.replace(/s+/g,"");       
去除两头空格:   
str   =   str.replace(/^s+|s+$/g,"");
去除左空格:
str=str.replace( /^s*/, '');
去除右空格:
str=str.replace(/(s*$)/g, "");

只允许输入数字(整数:小数点不能输入)
<input type="text" onkeyup="value=value.replace(/[^d]/g,'')" > 
允许输入小数(两位小数)
<input type="text" onkeyup="value=value.replace(/^D*(d*(?:.d{0,2})?).*$/g, '$1')" > 
允许输入小数(一位小数)
<input type="text" onkeyup="value=value.replace(/^D*(d*(?:.d{0,1})?).*$/g, '$1')" > 
开头不能为0,且不能输入小数
<input type="text" onkeyup="value=value.replace(/[^d]/g,'').replace(/^0{1,}/g,'')" >
原文地址:https://www.cnblogs.com/zyg316/p/9304106.html