日期:2014-05-16 浏览次数:20360 次
var getCharByTagName = function(str, tagName){ var reg = /\[([^\]]+)\](.*)\[\\(\1)\]/; var result; if((result = reg.exec(str)) && result[1] === tagName) { return result[2]; } return false; } console.log(getCharByTagName('[cn]中文[\\cn]', 'cn'));
------解决方案--------------------
function getMatchs(str,tag){
var reg =eval('/\\['+tag+'\\](.+?)\\[\\/'+tag+'\\]/i');
return str.match(reg);
}
var m = getMatchs('aaaa[cn]中文[/cn]bbbb[en]英文[/en]ccccc','cn');
alert(m.length +"\r\n" +m[0]+"\r\n"+m[1]);
------解决方案--------------------
function getResult(str, tagName) { var regex = "\\["+tagName+"\\]([^\\[]*)\\[/"+ tagName +"\\]"; var oRegex = new RegExp(regex,'ig'); var result = []; var match; while(match = oRegex.exec(str)) { result.push(match[1]); } return result; } function getCharByTagName(str,tagName) { var result = getResult(str,tagName); if(result.length > 0) return result.join(','); else return '没有匹配的结果'; } var result = getCharByTagName('[en]中文[/en][en]英文[/en]','en'); alert(result)