Regular Expressions In javascript



This document covers the basic differences between perl and javascript in terms of regular expressions.



Regular Expressions On The Fly
Create a regular expression object, use the regular expression object.
You can use variables to construct the expression.

e.g. (replacing names with more descriptive versions of the name)
var name = "Richard";
var str = "Once upon a time, Richard conquered Europe."

var regex = new RegExp(name, "g");
str = str.replace(regex, "Sir " + name  + ", the great, ");

str is now: "Once upon a time, Sir Richard, the great, conquered Europe."

e.g. (primitive email check)
var email = new RegExp("\w*@\w*\.\w*");
var is_valid = email.test("vascy@hotmail.com");



Replacing Text With Info From Any Array
var Characters = new Array();
    Characters[0] = new Object();
    Characters[0].name = "Timmy";
    Characters[0].hp = 40;
    Characters[0].maxhp = 50;
    Characters[1] = new Object();
    Characters[1].name = "Jimmy";
    Characters[1].hp = 45;
    Characters[1].maxhp = 55;

var str = "[0.name] rocks because he has [0.hp] out of [0.maxhp] but [1.name] is better with [1.hp] out of [1.maxhp]";
var regex = /\[([0-9]+).([^\[\]]+)\]/;

while (info = regex.exec(str)) {
  var index = info[1];
  var prop = info[2];
  str = str.replace(regex, Characters[index][prop]);
}

str becomes: "Timmy rocks because he has 40 out of 50 but Jimmy is better with 45 out of 55"



Splitting a string down into seperate parts (and then creating an object from it)
The following code takes a string parameter like "x=4;y=56;name=test;" and converts it into an object.
function StringToObject(str) {
  var tokens = str.split(";");

  var object = new Object();
  for (var i = 0; i < tokens.length; i++) {
    var property = tokens[i].substring(0, tokens[i].indexOf("="));
    var value = tokens[i].substring(tokens[i].indexOf("=") + 1, tokens[i].length);
    object[property] = value;
  }

  return object;
}

The reverse, object to string...
function ObjectToString(object) {
  var str = "";

  for (var property in object) {
    str += property + "=" + object[property] + ";";
  }

  return str;
}



Finally, a html related thing...
To strip bracket's from some text, you can do:
var reg = new RegExp("[.*]", "g");

var str = "[hello]blah[/hey]";
str.replace(reg);
The important bit here is: var reg = new RegExp("[.*]", "g");
Within "[.*]" you can note that:
. = any character and .* = any character, any number of times
And the "g" means globally replace.
An "i" here would be case-insensitive matching...
and "gi" would be globally-case-insensitive matching...

(Bracket's don't have a case, so that's why I don't have "i" listed...)

Note that I've used [ and ] instead of the brackets that html actually uses...



Examples! :)
What The Regular Expression Does In Wordsperljavascript
Replace the "old" with the "new" throughout a string$str =~ s/old/new/gstr = str.replace(/old/, "new")
Find the position of the first number within a string$pos = $str =~ m/[0-9]/ pos = str.search(/[0-9]/);
Swap any occurance of "jim" and "bob" anywhere within a string$str =~ s/(jim|bob)(.*?)(jim|bob)/$3$2$1/str = str.replace(/(jim|bob)(.*?)(jim|bob)/, "$3$2$1")



Links:
http://developer.netscape.com/docs/manuals/js/client/jsguide/regexp.htm
http://www.johnrobertmorris.com/dev/Regex.asp http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/