Prototype Methods

RegExp.prototype.exec()

Executes a search for a match in its string parameter. It is the reverse of String.prototype.match().

RegExp.prototype.test()

Tests for a match in its string parameter.

RegExp.prototype.toString()

Returns a string representing the specified object. Overrides the Object.prototype.toString() method.


var r = /a/gi;
console.log(r.exec("123abcABC"));
console.log(r.test("123abcABC"));
console.log(r.toString());

["a", index: 3, input: "123abcABC"] true /a/gi

This standard function allows you to retrieve the query strings in a URL.


function getParameterByName(name, url) {
  if (!url) url = window.location.href;
  name = name.replace(/[\[\]]/g, "\\$&");
  var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var foo = getParameterByName('foo');

The following standard function validates an email address.


function validateEmail(email) {
  var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}