我有很多字符串,比如
var str="#{ZyDt8spGZOA.mI8uHaT7o47}+#{z3PENrRf0cn.mI8uHaT7o47}+#{lhUBSvCMPWu.mI8uHaT7o47}+#{YNpiWU7fw9m.mI8uHaT7o47}";
我需要只将花括号内的内容添加到数组中。 (在上面的长度为4的情况数组中)。
我怎样才能做到这一点?
答案 0 :(得分:1)
array = str.split(“} +#{”);
这样做之后reult会看起来像
array[0] = "#{ZyDt8spGZOA.mI8uHaT7o47"
array[1] = "z3PENrRf0cn.mI8uHaT7o47"
array[2] = "lhUBSvCMPWu.mI8uHaT7o47"
array[3] = "YNpiWU7fw9m.mI8uHaT7o47}"
之后只需删除额外的字符串形式array [0]和array [3]
array [0] = array [0] .substring(2,array [0] .length)
array [3] = array [3] .substring(0,array [3] .length-1)
答案 1 :(得分:1)
var found = [],
rxp = /{([^}]+)}/g,
str = "#{ZyDt8spGZOA.mI8uHaT7o47}+#{z3PENrRf0cn.mI8uHaT7o47}+#{lhUBSvCMPWu.mI8uHaT7o47}+#{YNpiWU7fw9m.mI8uHaT7o47}",
mat;
while( mat = rxp.exec( str ) ) {
found.push(mat[1]);
}
alert(found);