Most variables in JavaScript represent themselves internally as strings. The String class in JavaScript contains a number of handy methods, but many commonly-used functions which are available in other languages, such as the trim() function, are notably missing from JavaScript.

Since strings are represented as JavaScript classes, it is possible to extend the functionality of the basic String class in order to add those methods which are missing. This article shows how to do this with a few simple lines of JavaScript which can be placed in a <SCRIPT> block at the top of one’s HTML page.


trim()

The trim() method removes white spaces from the beginning and end of a string. Here is the prototype extension which will add this to the JavaScript String class:

The trim() prototype extension

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

Usage Example: outputs ‘This is a test’

var x="    This is a test   ";
document.write(x.trim());


replaceAll()

The JavaScript replace() function is powerful, uses regular expressions and has a lot of innate functionality. However, it’s very hard to replace every occurrence of one string inside another in one go using the standard replace() method, since if the string you’re replacing contains anything the parser views as a regular expression, your replace attempt will fail.

Another method of doing this is to split the string into an array, based on the substring you’re replacing, and then join it again, based on the replacement. Here’s a prototype which does that:

The replaceAll() prototype extension

String.prototype.replaceAll=function(x,y) { return this.split(x).join(y); };

Usage Example: outputs ‘This is a new string. Its value is a new one.’

var x=”This is an old string. Its value is an old one.”;
document.write(x.replaceAll("an old","a new"));


count()

This prototype allows you to count how many times one string occurs within another:

The count() prototype extension

String.prototype.count=function(x) { return this.split(x).length-1; };

Usage Example: outputs ’4′

var x="There is new territory";
document.write(x.count("e"));


wordCount()

This prototype counts how many words there are in a string. It does this by using a regular expression to replace each word with the string ~WoRd~ and then counting how many times that string occurs:

The wordCount() prototype extension

String.prototype.wordCount=function()
{
return this.replace(/\w+/g,"~WoRd~").count("~WoRd~");
};

Usage Example: outputs ’8′

var x="How many words are there in this sentence?";
document.write(x.wordCount());


capitalize()

Here’s a handy function to capitalize the first letter of every word in a string. It uses a regular expression to parse each word out of the string and pass it through a helper function which capitalizes its first letter (and converts the rest of the string to lower case):

The capitalize() prototype extension

String.prototype.capitalize=function()
{
return this.replace(/\w+/g,
function(a){return a.charAt(0).toUpperCase()+a.substr(1).toLowerCase();});
};

Usage Example: outputs ‘This Function Is Working’

var x="this FUNCTION is working";
document.write(x.capitalize());


isEmailAddress()

This function checks whether a string is a valid email address. It’s a very handy thing to have if you’re using JavaScript to validate form input:

The isEmailAddress() prototype extension

String.prototype.isEmailAddress=function()
{
var ap=this.indexOf("@");
var ap2=this.lastIndexOf("@");
var dp=this.lastIndexOf(".");
var elen=this.length-1;
return (ap>0) && (ap<elen) && (dp>ap) && (dp<elen)
&& (ap==ap2) && (this.charAt(ap+1)!=".");
}

Usage Example: outputs ‘true’

var x="person@domain.com";
document.write(x.isEmailAddress());

Usage Example: outputs ‘false’

var x="person@.com";
document.write(x.isEmailAddress());


This concludes the String Prototype Extensions article. If I come across or think of more prototyping functions, I will add them here. If you think of any that I’ve missed, feel free to email them to me, and I will include them in this article. [Dan Sutton]