String.prototype.tld – match Top-Level-Domain
1 2 String.prototype.tld = function(){ 3 return (m = this.match(new RegExp("\.([a-z,A-Z]{2,6})$") )) ? m[1] : false; 4 }
DZone Snippets > featurelover > javascript
13375 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Jochen Preusche http://www.jochenpreusche.de
1 2 String.prototype.tld = function(){ 3 return (m = this.match(new RegExp("\.([a-z,A-Z]{2,6})$") )) ? m[1] : false; 4 }
1 2 function lineBreakCount(str){ 3 /* counts \n */ 4 try { 5 return((str.match(/[^\n]*\n[^\n]*/gi).length)); 6 } catch(e) { 7 return 0; 8 } 9 }
1 2 function currency2float(m){ 3 var pattern = /[\.,][-|–]?$/; /* 100.-- >>> 100 */ 4 /* 100,- >>> 100 */ 5 var m = m.replace(pattern, ""); 6 7 var pattern = /[\.,]([\d]{0,2})$/; /* 100,95 >>> 100X95 */ 8 var m = m.replace(pattern, "X$1"); 9 10 var pattern = /[\.',]/g; /* delete ' , . */ 11 var m = m.replace(pattern, ""); 12 13 var m = m.replace(/X/g, '.'); /* 100X95 >> 100.95 */ 14 15 if(isNaN(m*1)) return false; 16 return(m*1); 17 }
1 2 function getparams(s){ 3 a = s.match(/[^&?=]*=[^&?=]*/g); 4 r = new Array(); 5 for (i=0; i<a.length; i++) { 6 r[i] = new Array(a[i].match(/[^&?=]*/)[0], a[i].match(/=([^&?]*)/)[0].replace('=', '')); 7 } 8 return(r); 9 }
1 2 function getvars(s){ 3 // php preg_match would be as easy as: (/([^&?=])=[^&?=]/g) 4 a = s.match(/[^&?=]*=[^&?=]*/g); 5 r = new Array(); 6 for (i=0; i<a.length; i++) { 7 r[i] = a[i].match(/[^&?=]*/)[0]; 8 } 9 return(r); 10 } 11 /* test */ 12 alert(getvars(window.location.search));
1 2 /* javascript */ 3 String.prototype.get = function(p){ 4 return(this.match(new RegExp("[?|&]?" + p + "=([^&]*)"))[1]); 5 } 6 7 /* test: yourfile.html?test=test234 alerts: test234 */ 8 alert(window.location.search.get('test'));