Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL
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
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Tips and TricksImage ReplacementUsing IncludesForm ValidationDebugging
Got any JavaScript Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.
Recommended Reading
Javascript the definitive guide

Javascript and DHTML cookbook

Pro javascript techniques

simply javascript