1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
<script type='text/javascript'>
/**************************************
* Script prototype match_all *
* © Spacefrog 2010 *
* emulation du preg_match_all de php *
**************************************/
RegExp.prototype.match_all=function(mystring){
var res=[]
var match;
if (arguments.length>1){
var i=0;
while(arguments[++i]!= undefined){
res['$'+arguments[i]]=new Array()
}
while (match = this.exec(mystring)) {
var i=0;
while(arguments[++i]!=undefined){
match[+arguments[i]] && res['$'+arguments[i]].push(match[+arguments[i]]);
}
}
}
else{
while (match = this.exec(mystring)) {
res.push(match[0]);
}
}
return res;
}
//Exemple d'utilisation:
var chaine="produit1 : 321321654321, produit2 :54231fds , produit3 : dfaaa2-22f"
reg=/(\w+)\s*:\s*([^,]+)/gi;
results=reg.match_all(chaine,1,2)
alert(results.$1 +'\n' + results.$2)
</script> |
Partager