jQuery RegEx Examples to use with .match()

Sam Deering

This is a reference post for some common RegExp (regular expressions) selectors that can be used with the jQuery.match() function. This is very handy for finding pretty much anything inside your web page text and then doing something neat with it. Also handy for validation on forms.

Learn how to use Regex with these examples:

Regex Cheat Sheet

jQuery RegExp for Numbers

//select integers only
var intRegex = /[0-9 -()+]+$/;   
//match any ip address
var ipRegex = 'bd{1,3}.d{1,3}.d{1,3}.d{1,3}b';  
//match number in range 0-255
var num0to255Regex = '^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$';
//match number in range 0-999 
var num0to999Regex = '^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$';
//match ints and floats/decimals
var floatRegex = '[-+]?([0-9]*.[0-9]+|[0-9]+)'; 
//Match Any number from 1 to 50 inclusive
var number1to50Regex = /(^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$)/gm; 

jQuery RegExp for Validation

//match email address
var emailRegex = '^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$'; 
//match credit card numbers
var creditCardRegex = '^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35d{3})d{11})$'; 
//match username
var usernameRegex = '/^[a-z0-9_-]{3,16}$/'; 
//match password
var passwordRegex = '/^[a-z0-9_-]{6,18}$/'; 
//Match 8 to 15 character string with at least one upper case letter, one lower case letter, and one digit (useful for passwords).
var passwordStrengthRegex = /((?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,15})/gm; 
//match elements that could contain a phone number
var phoneNumber = /[0-9-()+]{3,20}/; 

jQuery RegExp for Dates

//MatchDate (e.g. 21/3/2006)
var dateRegex = /(d{1,2}/d{1,2}/d{4})/gm; 
//match date in format MM/DD/YYYY
var dateMMDDYYYRegex = '^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)dd$'; 
//match date in format DD/MM/YYYY
var dateDDMMYYYRegex = '^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)dd$'; 

jQuery RegExp for URL’s

//match a url
var urlRegex = '/^(https?://)?([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?$/'; 
//match a url slug (letters/numbers/hypens)
var urlslugRegex = '/^[a-z0-9-]+$/'; 
//match a url string (Fixes spaces and querystrings)
var urlRegex = /(https?://)?([da-z.-]+).([a-z.]{2,6})([/w.-=?]*)*/?/

jQuery RegExp for Vowels

//select vowels only
var vowelRegex = /^[aeiou]/;   

jQuery RegExp for Whitespace

//select whitespace
var whiteSpaceRegex = '^[ t]+'; 
//select whitespace and tabs
var whiteSpaceRegex = '^[ t]+|[ t]+$';  
//select whitespace and linebreaks
var whiteSpaceRegex = '[ trn]';  
//replace newline characters with <br /> tags
newLineToBr = function(str) { return str.replace(/(rn|[rn])/g, '<br />'); } 

jQuery RegExp for Domain Names

//match domain name (with HTTP)
var domainRegex = /(.*?)[^w{3}.]([a-zA-Z0-9]([a-zA-Z0-9-]{0,65}[a-zA-Z0-9])?.)+[a-zA-Z]{2,6}/igm; 
//match domain name (www. only) 
var domainRegex = /[^w{3}.]([a-zA-Z0-9]([a-zA-Z0-9-]{0,65}[a-zA-Z0-9])?.)+[a-zA-Z]{2,6}/igm; 
//match domain name (alternative)
var domainRegex = /(.*?).(com|net|org|info|coop|int|com.au|co.uk|org.uk|ac.uk|)/igm; 
//match sub domains: www, dev, int, stage, int.travel, stage.travel
var subDomainRegex = /(http://|https://)?(www.|dev.)?(int.|stage.)?(travel.)?(.*)+?/igm;

jQuery RegExp for Images

//Match jpg, gif or png image	
var imageRegex = /([^s]+(?=.(jpg|gif|png)).2)/gm; 
//match all images
var imgTagsRegex = /<img .+?src="(.*?)".+?/>/ig;  
//match just .png images
<img .+?src="(.*?.png)".+?/>  

Other Useful jQuery RegExp Examples

//match RGB (color) string
var rgbRegex = /^rgb((d+),s*(d+),s*(d+))$/;  
//match hex (color) string
var hexRegex = '/^#?([a-f0-9]{6}|[a-f0-9]{3})$/'; 
//Match Valid hexadecimal colour code
var hexRegex = /(#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?)/gm; 
//match a HTML tag (v1)
var htmlTagRegex = '/^< ([a-z]+)([^<]+)*(?:>(.*)< /1>|s+/>)$/'; 
//match HTML Tags (v2)
var htmlTagRegex = /(< (/?[^>]+)>)/gm; 
//match /product/123456789
var productUrlRegex = '(/product/)?+[0-9]+';  
//Match Letters, numbers and hyphens
var lnhRegex = /([A-Za-z0-9-]+)/gm;  
//match all .js includes	
var jsTagsRegex = /<script .+?src="(.+?.js(?:?v=d)*).+?script>/ig;  
//match all .css includes
var cssTagsRegex = /<link .+?href="(.+?.css(?:?v=d)*).+?/>/ig;  

These patterns are intended for reference purposes and many have not been extensively tested. Please use with caution and test thoroughly before use.

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • roy

    Awesome

  • Pingback: New additions to the Regex Examples Post

  • Mamoon

    Hello

    I am newbe in regex.
    I am little bit confused about when you use /regEx/ and when ‘/regEx/’ and when ‘regEx’

    what pattern should i follow to write regEx inside ” or // or ‘//’

    • jquery4u

      @Mamoon,

      Bascially the only format you need is /regez/igm when declaring a regex pattern to a variable for use with .match() .test() or .exec().

      Like this:

      var regex = /patternhere/igm;

      So this will do a incasesensitive global multiline search.

      The other patterns can also be quoted but this is optional and has no benefit. Apart from ‘regex’ which will just declare a string.

      I will be doing a post on how to use regex soon. This will show you easily how it all works. :)

  • Pingback: RegExp « 天機の

  • Brian

    Just to clarify, none of these are “jQuery functions” in any sense. Not a single one of the methods described are a part of the jQuery library. They are all built-in javascript methods and will work whether you have jQuery loaded or not.

    That being said, it’s a helpful reference despite the mistaken info.

  • http://jsfunctions.mondialdeal.com/ HappyLDE

    Awesome, thank you for all that

  • Pingback: The World In Links (1/16-1/22/2012) - Art, Gadgets, PHP

  • http://linesforme.blogspot.com/ james

    this is wonderful i’m beginning to use regular expression in every of my plugins thanks for sharing …

    • asdfasdf

      asdfasdf

  • Nat

    Really awesome! Thanks for tips)

  • Jignesh Desai

    Awesome!!! One question though, in the password strength regex have one additional minimum one special character requirement. So for that what would be the regex pattern?

  • Jignesh Desai

    this worked for me – /((?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]))/gm

  • Abdul Wahab Qambrani

    Really Awsome & Superbbb Sharing…….
    Thanks

  • ravi

    how to match integer and float numbers ?
    ..example: my expression should match 100,100.23,1,12 …any number between 0 to 100..

  • karthiik

    can any one post fax number regExP..

    • Ravi S

      function validFaxOrPhone(ObjectId) {
      var vvalue = trim($(“#” + ObjectId).val());
      var vRegExp = /^D?(d{3})D?D?(d{3})D?(d{4})$/;
      if (vvalue.match(vRegExp) == null)
      return false;
      else return true;
      }

      • venu

        i want to match the regular exp and the value which i give. Can u please help me to do this in jquery.
        Thanks in advance

  • bhargav

    not working properly i have test first int regex but it alloed 123gf21 as number

  • nick

    You post regest search strings with the title jquery but fail to show how to implement it in jquery. Fail write up.

  • bibin

    thanks a lot

  • Chanikya

    Hi what about DD/MM/YYYY format for February

  • andhy1272

    //match ip address
    var ipRegex = ‘^([01]?[0-9][0-9]|2[0-4][0-9]|25[0-5]).([01]?[0-9][0-9]|2[0-4][0-9]|25[0-5]).([01]?[0-9][0-9]|2[0-4][0-9]|25[0-5]).([01]?[0-9][0-9]|2[0-4][0-9]|25[0-5])$';

    • andhy1272

      Correcting myself. The last one doesn’t work with a single number. Here’s the correct way:
      //match ip address
      var ipRegex = ‘^([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])$';

  • http://www.facebook.com/profile.php?id=1457796542 Saheel Ahmed

    Good Work

  • Lazaro Fernandes Lima

    instead of

    var urlRegex = ‘/^(https?://)?([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?$/';

    use

    var urlRegex = /(https?://)?([da-z.-]+).([a-z.]{2,6})([/w.-=?]*)*/?/

    Fixes spaces and querystrings

    • http://jquery4u.com/ jQuery4u

      Thanks Lazaro, I’ll update.

  • email

    the email regex has also an error, it only allows CAPS, you should also include “a-z”, so the email regex would be:

    var emailRegex = ‘^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$';

  • AoiChan

    For the emails, this is better to accept lowercase too :)

    var emailRegex = ‘^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$';

  • Pingback: Regular Expressions Resources | TechSlides

  • http://www.newventurewebsites.com/ Michael Sadler

    Awesome thanks! Hopefully the ones I use work. :)

  • Arun Sadanandan

    Hi,

    I was searching for float-only numbers RegEx and came across your website. But the RegEx given over here accepts letters too. So I came across this RegEx which works perfectly for me…May be you can check and update on your site?

    /(^-?dd*.d*$)|(^-?dd*$)|(^-?.dd*$)/

    Thanks

  • XCoder

    Hi , I think the Username part is not ok as Lower case letters only. A username can be in Upper case letters as well, and in that case I feel it should not be added as a Lower case only! Hope that can be fixed because I needed it and I had problems figuring out why… So for those who wants to use from A to Z in Uppercase and Lowercase Letters + 0 to 9 in Letters and + Underscore + Dash.. then here is the code >> /^[a-z-A-Z0-9_-]{3,25}$/

  • Varun Nath

    Just tried the email regex. You seem to have missed the ‘i’ Modifier to give you case insensitive matching.

  • Saas

    jab se tere naina mere nainon se laage re…

  • Davy

    For emailadres is this the best way:

    function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(\([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/i);
    return pattern.test(emailAddress);
    };

    then you do:

    if(!isValidEmailAddress(YOUREMAILADRES))
    {
    // HERE YOUR ACTION
    }

  • Rakhi Dhavale

    awesome thanks a lot!