/**
*x00-xff是指所有ASCII,即单字符,/[^\x00-xff]/g指所有非单字符,包含全角字符,汉字
\u4e00-\u9fa5匹配所有的汉字,只匹配汉字,不包含全角字符,全角字符也占两个字节
/
var strDo = {
getByteLength:function(str){
if(typeof str !== 'string') return 0
return str.replace(/[^\x00-xff]/g,' ').length
},
//检查值是否字母,数字,汉字,且字节长度是大于6小于32
checkVal:function(val){
var reg = /^[a-zA-Z\d\u4e00-\u9fa5]+$/
if(reg.test(val)){
var len = this.getByteLength(val)
return len >6 &&len <32
}else{
return false
}
},
//截取等长字符串
equalLengthStr:function(str,len){
if(typeof str !== 'string') return ''
var len = len || 99999,
tempStr = '',
n = 0
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"
开头和结束符不需要吧,把^和$删掉,你两个尖括号直接用就行了,加方括号干嘛。而且.*只会匹配最多的字符,所以这样写并不会匹配C或G,而会匹配第一个和最后一个尖括号,你用.*?试一试。所以我觉得用<(.*?)>就行了。仅供参考