Ruslan Ulanov’s Codeshack

The developer’s notebook

Useful JavaScript Regular Expression Functions

leave a comment

Here’s a few links for commonly used REGEXs..

10+ Useful JavaScript Regular Expression Functions to improve your web applications efficiency

Check that an input string looks like a valid email address.

function isEmail (s) {
  var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
  return String(s).search (isEmail_re) != -1;
}

Returns a string with everything but the digits removed. Could be used to validate phone numbers, etc.

function getDigits (s) {
  return s.replace (/[^\d]/g, “”);
}

Written by Ruslan Ulanov

September 4th, 2009 at 12:16 pm

Posted in JavaScript

Tagged with ,