DevelopPHP
home gear home donate
Search Results For
in:

Libraries book

HTML Library CSS Library JavaScript Library PHP Library

Videosbook

PHP and MySQL JavaScript HTML and CSS Vector and 3D Flash Actionscript Android Dev Miscellaneous

Resourcesbook

Website Hosting Developer Q+A
Sprite Programming Mastery
RegExp Object Javascript

Javascript RegExp objects allow authors to work with regular expressions in Javascript. Regular expressions work similarly in most scripting languages. We supply them with two paramters: (1)the text pattern to match for, and (2)optional flags. Special characters can also be applied to your patterns for enabling a wide range of logic within your regular expressions.

Regular expressions are used with several String methods of Javascript such as match(), replace(), search(), split(). RegExp objects sport built-in properties, methods and special characters that you can explore on this page.

 

RegExp Object Syntax

Javascript CODE EXAMPLE
var regex = /pattern/;
var regex = new RegExp("pattern");

Both approaches above will establish a similar regular expression in Javascript. The literal approach uses forward slashes around the text pattern, while the constructor approach uses quote marks and separates the paramters. If you know the pattern is static and will not change use a literal regular expression to maximize script performance. If you know that the pattern will be dynamic, use the second approach that calls the constructor and explicitly creates a RegExp object.

This next example shows applying flags to your regular expression. We apply a global(g) flag along with an ignore case(i) flag to the regular expression so that all matches of the word "bird" are found and the text casing is not a factor in the pattern match.

Javascript CODE EXAMPLE
var regex = /bird/gi;
var regex = new RegExp("bird","gi");

 

RegExp Flags

You can apply one or more flags to your patterns in order to alter the match logic.

g - (global) target all matches

Javascript CODE EXAMPLE
var str = "Joe sells cogs from his cog store.";
var regex = /cog/g;
var result = str.replace(regex, "bed");
document.write(result); // Joe sells beds from his bed store.

i - ignore the casing of letters

Javascript CODE EXAMPLE
var str = "I want to fIGhT.";
var regex = /fight/i;
var result = str.replace(regex, "fart");
document.write(result); // I want to fart.

m - (multiline) performs match over multiple lines("\n" represents a new line)

Javascript CODE EXAMPLE
var str = "pop pop\npop pop";
var regex = /p$/gm;
var result = str.replace(regex, "X");
document.write(result); // pop poX pop poX

 

RegExp Special Characters

Consider these placeholder values before you explore the special characters:
abc
= primary pattern to match, replace 'abc' with your pattern.
i / j = integers, replace 'i' and 'j' with integers where you see them below.

\ - escapes the normal functionality of special characters to interpret them literally
Syntax: /abc\+/

^ - match pattern where it is the beginning of input (newlines affected using the g and m flags)
Syntax: /^abc/

$ - match pattern where it is the end of input (newlines affected using the g and m flags)
Syntax: /abc$/

* - match pattern 0 or more times
Syntax: /abc*/

+ - match pattern 1 or more times
Syntax: /abc+/

? - match pattern 1 or 0 times
Syntax: /abc?/

. - match everything except newline(\n) characters
Syntax: /./

( ) - match and store into resulting array
Syntax: /(abc)/

(?: ) - match and do not store into resulting array
Syntax: /(?:abc)/

(?= ) - match 'abc' only if it precedes 'def'
Syntax: /abc(?=def)/

(?! )- match 'abc' only if it does not precede 'def'
Syntax: /abc(?!def)/

| - match either 'abc' or 'def'
Syntax: /abc|def/

{i} - match the pattern 'i' times
Syntax: /abc{i}/

{i,} - match the pattern at least but not limited to 'i' times
Syntax: /abc{i,}/

{i,j} - match the pattern at least 'i' times, and at the most 'j' times
Syntax: /abc{i,j}/

[ ] - match the range of characters specified within the brackets
Syntax: /[hijklmnop]/ same as /[h-p]/

[^ ] - match what is not in the character set
Syntax: /[^hijklmnop]/ same as /[^h-p]/

\b - match where 'abc' starts a new word
Syntax: /\babc/

\B - match where a word ends with 'abc'
Syntax: /\Babc/

\c - match a control character ( A - Z )
Syntax: /\cA/

\d - match digit found in a string, same as using [0-9]
Syntax: /\d/

\D - match non-digit found in a string, same as using [^0-9]
Syntax: /\D/

\f - match form feed characters
Syntax: /\f/

\n - match newline characters
Syntax: /\n/

\r - match carriage return characters
Syntax: /\r/

\s - match white space, newline, return and tab, same as using [ \n\r\t]
Syntax: /\s/

\S - match non-white space characters, same as using [^ \n\r\t]
Syntax: /\S/

\t - match tab characters
Syntax: /\t/

\v - match vertical tab
Syntax: /\v/

\w - match any alphanumeric character, same as using [a-zA-Z0-9_]
Syntax: /\w/

\W - match any non-alphanumeric character, same as using [^a-zA-Z0-9_]
Syntax: /\W/

 

Properties of the RegExp Object

lastindex - returns the next index position that follows the last match made

Javascript CODE EXAMPLE
var regex = /a/g;
var result = regex.test("cat");
document.write(result+" : "+regex.lastIndex); // true : 2

source - returns the text of the pattern without its forward slashes

Javascript CODE EXAMPLE
var regex = /abc/;
document.write(regex.source); // abc

 

Methods of the RegExp Object

exec - returns the matched pattern if it is found, and returns 'null' if not found

Javascript CODE EXAMPLE
var str = "Birds fly in the sky.";
var regex = /fly/;
var result = regex.exec(str);
document.write(result); // fly

test - returns 'true' if a match is found, and returns 'false' if not found

Javascript CODE EXAMPLE
var str = "abc 123";
var regex = /bc 12/;
var result = regex.test(str);
document.write(result); // true

toString - returns a string representing the RegExp object

Javascript CODE EXAMPLE
var regex = /abc/;
var result = regex.toString();
document.write(result); // /abc/

 

Home   •   Terms of Use   •   T-Shirts   •   RSS   •   Testimonials   •   Subscribe   •   Classroom Chalkboard   •   Developer Q+A   •   Donate   •   Top ↑
Popular In PHP / MySQL E-Commerce Store Production
Social Network Website Building
Image Upload / Photo Processing
CMS Software Programming
Mass Email Systems
Magic XML Data
Search Programming
Popular In JavaScript JSON Programming
Ajax Programming
Animating Elements
WYSIWYG Programming
Date/Time Programming
DOM Scripting
Object Reference
Popular In HTML Canvas Element
New Form Elements
Audio Element
Video Element
Drag and Drop
Event Handling
Element Grouping
Popular In CSS Custom Font Embedding
Dynamic Fit Backgrounds
Theatre Mode
Box Overlays
CSS Level 1 Properties
CSS Level 2 Properties
CSS Level 3 Properties
DevelopPHP.com is a growing educational system packed with video and written material that you can access 24/7 100% free. The focus here in 2014 is spread between five technologies: HTML, CSS, JavaScript, PHP and MySQL. Learn programming theory, database interaction, web design, animation, graphics editing, vector art, 3D modeling, and much more. The educational experience here is a hybrid one offering both high quality video tutorials and code libraries that grow more robust every day. The code libraries are being assembled so that developers can discover all of the building blocks of their favorite languages. The libraries coupled with high quality video tutorials will take you deep down the rabbit hole very quickly. You can also ask questions and get free advice at WebIntersect.com.
HTML5