87,987
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
String.prototype.getValue = function (key) {
var reg = new RegExp(key + ":\\s*(.+?)(;|$)");
var match = reg.exec(this);
if (match && match.length > 1) {
return reg.exec(this)[1];
}
else {
return '';
}
}
$(document).ready(function () {
var s = 'a1;ab;ce;kk:nanren;ii:虐人;备注:fef;';
alert(s.getValue('ab'));
});
</script>
function fun(key){
var a = "a1;ab;ce;kk:nanren;ii:虐人;备注:fef;".match(new RegExp(";"+key+":([^;]+)"));
return a ? a[1] : "";
}
//一定要用字符串么,用对象多好,可以按json格式来写
var obj = {"kk":"nanren","ii":"虐人","备注":"fef"};
function get(key)
{
return obj[key]||"";
}
alert(get("ii"));
alert(get("aaaa"));
function get(key)
{
var string = '';//字符串
var len = key.length + 1 ;
var start = string.indexof(key+':');
if(start >0)
{
var end = string.indexof(';',start );
return string.substring(start+len,end)
}
return '' ;
}
var s='a1;ab;ce;kk:nanren;ii:虐人;备注:fef;';
var getValue=function(key){
var reg=new RegExp(key+":\\s*(.+?)(;|$)");
var match=reg.exec(s);
if(match && match.length>1){
return reg.exec(s)[1];
}
else{
return '';
}
}
alert(getValue('ii'));