JavaScript Regex Class

The Vert Studios PHP Regex Class comes to JavaScript! Key Differences: While the method calls are identical between the PHP and JavaScript Regex classes (barring the static:: operator), the construction of the methods were quite different. JavaScript and PHP parse escape characters differently.

JavaScript Regex Class

The JavaScript Regex class provides methods for validating data. Currently, the class contains the following patterns (denoted as types in the class methods):
  • int: Integer
  • float: Float
  • mailstrings: Mail Strings (cc: to: bcc:, etc)
  • email: Email
  • html: HTML
  • url: URL
  • zip: Zip Code
  • alpha: Alphabetic Character
  • num: Number
  • bbcode: BB Code
  • usphone: Phone
  • usaddress: US Street Address
  • name: Name
  • fullname: Full Name(must be at least a first and a last name)
  • lastname: Last Name(allows for numerical suffixes such as "2nd")

Download the source code

The class is written so that you can easily add your own regular expression patterns. It's important to note that the Regex class consists of static methods. That means you do not need to create an instance of the Regex class to use the methods. The class contains 4 methods as part of the main interface:

Regex.is(type,value)

Description: returns boolean true if value is an exact match with the pattern associated with type. False if otherwise. Examples:
        console.log(Regex.is("email","hi@vertstudios.com"));
        console.log(Regex.is("int","A 100 dollar bill"));
        console.log(Regex.is("usphone","903-920-9514"));
        console.log(Regex.is("zip","75701"));
        console.log(Regex.is("float",57.10));
        
        //Output:
        //true
        //false
        //true
        //true
        //true

Regex.has(type,value)

Description: returns boolean true if value contains at least one instance of the pattern associated with type. False if otherwise. Examples:
        console.log(Regex.has("email","our email: hi@vertstudios.com"));
        console.log(Regex.has("int","A 100 dollar bill"));
        console.log(Regex.has("html","Jolly Jolly"));
        console.log(Regex.has("name","#@#$! Arthur"));
        console.log(Regex.has("float","I'm tired. Aren't you?"));
        
        //Output:
        //true
        //true
        //false
        //true
        //false

Regex.hasAny(types,value)

types is a string with various pattern types separated by a comma. Ex: "html,bbcode,int" Description: returns boolean true if value contains at least one instance of any pattern associated with types. False if otherwise. Examples:
        console.log(Regex.hasAny("email,usphone","our email: hi@vertstudios.com"));
        console.log(Regex.hasAny("html,bbcode",'<a href="lolspam.html">oeutaoeuaoeu</a>'));
        console.log(Regex.hasAny("html,int,float","Jolly Jolly"));
        
        //Output
        //true
        //true
        //false
(Note the convenient spam validation of a textarea form submission using Regex.hasAny("html,bbcode,mailstrings", textarea) )

Regex.getDescription(type)

Description: returns a string describing type You'll see in the source code that there are two objects, pattern and description. pattern takes the form type: "regexpattern", while description takes the form type: "description". description is where these strings are pulled from.*** The purpose of this method is mainly for convenient automation. Example:
        var myform = {name: "Joseph", email: "Joseph@@vertstudios.com",
                         usphone: "9033305057", zip: "75AA8"};
        
        //Iterate through the "form" and output which values are valid.
        for(var i in myform)
        {
            console.log( Regex.getDescription(i) + " is " +
                       (Regex.is(i, myform[i]) ? "valid" : "invalid") );
        }
        //Output:
        //Name is valid
        //Email is invalid
        //Phone is valid
        //Zip Code is invalid

Download the source code

If you find any bugs or have any questions/suggestions, let us know in the comments below! December 07, 2010
About the Author:

Joseph is the lead developer of Vert Studios Follow Joseph on Twitter: @Joe_Query
Subscribe to the blog: RSS
Visit Joseph's site: joequery.me