Javascript 字符串上的方法(性能好)
var str = "It's a long string."var length = 10
console.log( str.slice( 0, length ) ) // "It's a lon"
用正则(match)
var str = "It's a long string."var length = 10
var regExp = new RegExp( "^(.{0," + length + "}).*$" ) // /^(.{0,10}).*$/
console.log( str.match( regExp )[ 1 ] ) // "It's a lon"
用正则(replace)
var str = "It's a long string."var length = 10
var regExp = new RegExp( "^(.{0," + length + "}).*$" ) // /^(.{0,10}).*$/
console.log( str.replace( regExp, "$1" ) ) // "It's a lon"
\.\d{4,}$这是对应正则的小数部分,\.表示小数点,\d为数字,\d{4,}表示最少有4个数字,$表示字符串的结束,如果符合这个正则就表示在字符串最后至少有4位小数
reg=/^\d+\.\d{4,}$/
完整的数字正则可以像这样写