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"
var str = "BEGIN CONST a,b=10,c END IF a>10 && (a+c<20 || b>c) THEN RETURN true END ELSE RETURN false END END END"str=str.replace(/\s/g,"")//取掉空格
str=str.match(/IF(\S*)THEN/)[1]//正则取出IF和THEN之间的字符串
str=str.split("&&")//按照&&分组
var str_end=str[0].split('>') //按照>分组
alert(str_end)//最终结果为str_end[0]=a str_end[1]=10
js正则表达式 /\$\{([A-Za-z]+?)\}/g 取第一捕获组的数据
我给你一个Javascript语言的例子,你看看吧.
<script type=text/javascript>var str="温馨提示:${KSSC}啊啊啊${YAAC}额额额${YWZH},是是是 ${CXMM},于${JDWCSJ}完成${LCJD}高规格,时间为${YJWCSJ}!"
var regex=/\$\{([A-Za-z]+?)\}/g
var result
while((result=regex.exec(str))!=null){
alert(result[1])
}
</script>
运行结果
KSSC
YAAC
YWZH
CXMM
JDWCSJ
LCJD
YJWCSJ