Examples of Regular Expressions

Examples of Regular Expressions

  1. Trim string in JavaScript

    var trimre=’^s+|s+$’; // replace whitespace
    var trimmedstring = string.replace(trimre,””);

  2. Check for a valid date format in JavaScript

    var re=’^(d{1,2})/(d{1,2})/(d{4})$’; // dd/mm/yyyy
    if (!datestring.match(re))
    {
    alert(“Date” + datestring + ” not in valid dd/mm/yyyy format”);
    }
    else // valid date keyed in
    {
    …valid date keyed
    }

  3. More to follow…

Leave a Reply

Your email address will not be published. Required fields are marked *