Never been to DZone Snippets before?

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

About this user

Jochen Preusche http://www.jochenpreusche.de

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

String.prototype.tld – match Top-Level-Domain

// javascript : match tld in window.location.hostname (last 2-6 chars)
   1  
   2  String.prototype.tld = function(){
   3  	return (m = this.match(new RegExp("\.([a-z,A-Z]{2,6})$") )) ? m[1] : false;
   4  }

Javascript count line breaks

Returns the number of line breaks in a string.

   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  }

currency formatted string to number

Makes a currency-formatted string numeric
   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  }

Javascript getparams

similar to JavaScript GetVars Array and Parse GetVars with JavaScript this fetches all the get variables with corresponding values.
   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  }

JavaScript GetVars Array

Figure out which Get-Vars are defined using client side javascript regex:

   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));

Parse GetVars with JavaScript

There are many examples of how to parse get vars client side around, but most of them are using arrays and take much more lines of code and ram then nescesary. This one gives an elegant way to get values of given get parameters:

   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'));
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS