16mm Guide


Ride-On Trains

Other Scales


Web Design



Contact Me

The Javascript String Class

HTML Methods
String Object Methods
Regular Expressions


The Javascript String class consists of two groups of methods: some convert strings into HTML equivalents, and some operate in a conventional "string-object" kind of way.

I find it useful to separate out these two flavours of functionality; it makes it easier to find what I'm looking for.

HTML Converters
anchor(nnn) <a name="nnn">string</a>
big() <big>string<big>
blink() <blink>string<blink>
bold() <b>string<b>
fixed() <tt>string<tt>
fontcolor(col) <font color="col">string</font>
fontsize(s) <font size="s">string</font>
italics() <i>string<i>
link(url) <a href="url">string</a>
small() <small>string<small>
strike() <strike>string<strike>
sub() <sub>string<sub>
sup() <sup>string<sup>
String Manipulation
returns the character at the specified position.
returns the Unicode of the character at the specified position.
concat (str1, str2, ...strN)
joins all the supplied strings together.
fromCharCode(n1, n2, ... nX)
converts the specified unicode values into a string
indexOf(substr [,start])
returns the position of the first occurrence of substr within the string, or -1 if no such occurrence is found.
lastIndexOf( substr [,start])
as indexOf, but starts searching backwards from the end of the string or from position 'start'.
Returns the sub-string which matches the regex.
if regex contains round brackets to capture parts of the string, match returns an array. index 0 contains the string that matched, or null if nothing matched, index 1 contains the contents of the first round brackets, index 2 contains the contents of the second round brackets, if there were any, and so on.
replace(regex, newSubStr)
performs a replacement using regex and newSubStr. The modified value is returned (the original String is not changed)
Searches a string for a specified value
slice(start, end)
Extracts a part of a string and returns the extracted part in a new string
split( [delimiter] )
Splits a string into an array of strings, splitting on the delimiter string. If there is no delimiter, the returned array has only one element.
substr(start, length)
Extracts a specified number of characters in a string, from a start index
returns a version of the string with all characters in lower case
returns a version of the string with all characters in upper case
returns the "string datatype" of the String object

Regular Expressions

Regular Expressions are used in three String Methods: match(), replace() and search(). In all three methods, the regular expression is supplied using / as the quotation character: see the example below.

Below is a section of Javascript to demonstrate Regular Expressions.

For a match, sections can be saved using round brackets:

var str = "Hello World";
var m = str.match(/(Wor)(ld)/);
// m[0] will contain 'World'
// m[1] will contain 'Wor'
// m[2] will contain 'ld'
Slashes seem to be interchangable with double quotes.

Note that str.replace() does not change str. it returns the modified value.
Note also that we can use Regular expressions in an almost-perl-like way. The replace(/xyz/g, "abc") syntax is the same as perl's s/xyz/abc/g
var str = "I like computers";
var str = str.replace(/computers/g, "trains"); 
Click here to see the result. (opens a popup)

More information

String Object
Regular Expressions
Webreference.com on Javascript Regular Expressions
PHP's Perl-Compatible Regular Expressions (PCRE) manual page
Perl RegEx manual page