This is the list of mine top 10 JavaScript String.prototype extensions. If you want to you can post yours bellow.
This extension adds trim() function:
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'’); }
//test trim
test = ‘ testing trim ‘;
document.write (’"’ + test.trim() + ‘"’);
This extension splits the string by given separator and returns an array with trimmed items. It uses the trim() extension above:
String.prototype.splitrim = function(t){ return this.trim().split(new RegExp(’\\s*’+t+’\\s*’)) }
//test splitrim
test = ‘ testing , splitrim ‘;
var arr = test.splitrim(’,');
document.write (’"’ + arr[0] + ‘"’);
document.write (’"’ + arr[1] + ‘"’);
This extension escapes HTML in the string:
String.prototype.escHtml = function(){ var i,e={’&’:'&’,'<’:'<’,'>’:'>’,'"’:'"’},t=this; for(i in e) t=t.replace(new RegExp(i,’g'),e[i]); return t }
//test escHtml
test = ‘testing <b>escHtml</b>’;
document.write (test.escHtml());
This extension unescapes HTML in the string:
String.prototype.unescHtml = function(){ var i,e={’<’:'<’,'>’:'>’,'&’:'&’,'"’:'"’},t=this; for(i in e) t=t.replace(new RegExp(i,’g'),e[i]); return t }
//test unescHtml
test = ‘testing <b>unescHtml</b>’;
document.write (test.unescHtml());
This extension URL encodes the string:
String.prototype.urlEncode = function(){ return encodeURIComponent(this); }
//test urlEncode
test = ‘http://www.gmail.com’;
document.write (test.urlEncode());
This extension checks if the string is a valid email address:
String.prototype.isEmail = function () { var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); var matches = rx.exec(this); return (matches != null && this == matches[0]); }
//test isEmail
test = ‘test@gmail.com’;
document.write (test.isEmail());
This extension checks if the string is a valid URL address:
String.prototype.isURL = function () { var rx = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?"); var matches = rx.exec(this); return (matches != null && this == matches[0]); }
//test isURL
test = ‘http://www.gmail.com’;
document.write (test.isURL());
This extension checks if the string contains the passed as parameter value:
String.prototype.contains = function(t) { return this.indexOf(t) >= 0 ? true : false; }
//test contains
test = ‘Can you find me?’;
document.write (test.contains(’find me’));
This extension checks if the string begins with the passed as parameter value. The second parameter is for ignore case:
String.prototype.beginsWith = function(t, i) { if (i==false) { return (t == this.substring(0, t.length)); } else { return (t.toLowerCase() == this.substring(0, t.length).toLowerCase()); } }
//test beginsWith
test = ‘Can you find me?’;
document.write (test.beginsWith(’can you’, true));
This extension checks if the string ends with the passed as parameter value. The second parameter is for ignore case:
String.prototype.endsWith = function(t, i) { if (i==false) { return (t == this.substring(this.length - t.length)); } else { return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase()); } }
//test endsWith
test = ‘Can you find me?’;
document.write (test.endsWith(’Me?’, true));
JavaScript,
String.prototype,
Top 10
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.