JavaScript regular expressions Free Web Design Tutorials
Home   HTML CSS JavaScript PHP MySQL Usability Glossary
JavaScriptBasicsRunning a ScriptVariablesExpressions and OperatorsObjects.Properties.MethodsThe Date ObjectStringsJavaScript Regular ExpressionsRegular ExpressionsDefining RegExp PatternsBranches and ConditionsLoopsArraysArray MethodsSorting ArraysUser-Defined FunctionsCookiesWindowsFrames

Home > JavaScript > Regular Expressionsprinter version

JavaScript Regular Expressions

Regular expressions are used to match patterns within strings. A regular expression object is created then used with one of the string methods or RegExp methods available.

The regular expression object may be created by assigning a string literal to a variable or by using RegExp() constructor.

Using a Variable

This is similar to creating a string variable but instead of using quotes, the slash character is used as a delimiter.

Creating a Regular Expression using a Variable
<script language="javascript" type="text/javascript">
    // This will match any string that contains s
    var myregexp = /s/;
</script>


Using the RegExp() Constructor

This produces the same result but uses a different syntax.

Creating a Regular Expression using the RegExp() Constructor
<script language="javascript" type="text/javascript">
    // This will match any string that contains s
    var myregexp = new RegExp("s");
</script>


RegExp Attributes

By default regular expressions are case sensitive and only search for the first occurrence of a match in a string.

To alter this there are two attributes that may be used that are specified outside the expression itself.

  1. The i attribute. This specifies that the pattern matching should be case-insensitive.
  2. The g attribute. This specifies that the pattern matching should be global i.e. all matches should be found, not just the first.


Using the i and g attributes
<script language="javascript" type="text/javascript">
    // Match any string that contains s globally and case-insensitive
    var myregexp = new RegExp("s","i","g");

    // This does the same
    var myregexp = /s/ig;
</script>


Using String Methods

Strings support a number of methods that can use regular expressions. Two of most useful are shown below.

  1. The search() method takes a regular expression as an argument and returns the position of the start of the first matching substring or -1 if no match is found.
  2. The replace() method takes a regular expression and a replacement string as arguments and returns the altered string if a match is found or the unaltered string if no match is found. Only the first match is altered unless the global attribute is used.


The Search() Method with a RegExp
<script language="javascript" type="text/javascript">
    // Find the first occurrence of a regular expression
    var mystring = "JavaScript";
    var myregexp = new RegExp("a");
    // This prints out 1
    document.write(mystring.search(myregexp));
</script>


The Replace() Method with a RegExp
<script language="javascript" type="text/javascript">
    // Search and replace a substring
    var mystring = "JavaScript";
    var myregexp = new RegExp("v");
    // This prints out JaffaScript
    document.write(mystring.replace(myregexp,"ff"));
</script>


Using RegExp Methods

RegExp objects have several methods for carrying out pattern matching that are similar to the string methods. One of the most useful one is show below.

The test() method takes a string argument and returns true if the string contains a match with the regular expression.

The RegExp Test() Method
<script language="javascript" type="text/javascript">
    // Find a match for a regular expresson
    var mystring = "JavaScript";
    var myregexp = new RegExp("v");
    // This prints out true
    document.write(myregexp.test(mystring));
</script>


Previous - JavaScript Strings Previous - Strings     Next - Defining RegExp Patterns Next - JavaScript Defining RegExp Patterns


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2009 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2009
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Recommended Reading
Javascript the definitive guide

Javascript and DHTML cookbook

Pro javascript techniques

simply javascript