JavaScript: RegExp Tutorial

By Xah Lee. Date: . Last updated: .

RegExp stands for “regular expression”. It allows you to check if a string has certain pattern, or replace parts of string by pattern.

Basic Example of Checking Email Pattern

Let's say you have a string, and you want to know if it contains repeation of letter o.

// check if a string contains a pattern

var myString = "search google";

// check if the string contains at least 2 or more repeated o
console.log(
    myString.search( /oo+/ )
); // 8
// return value is the start position of match

// check if the string contains “z”
console.log (
    myString.search ( /z/ )
 ); // -1
// if not found, returns -1

The /oo+/ is a RegExp pattern.

The syntax /…/ is a literal expression for RegExp.

In between the slashes, is the regexp pattern.

The regexp pattern /oo+/ means, the character o, followed by 1 or more o, The + means 1 or more of the preceding pattern.

〔►see JavaScript: RegExp Syntax

The line myString.search( … ) means search the pattern in myString.

search is a method of string prototype. It returns a number that's the first character's index where the pattern matches. If the pattern is not found, it returns -1.

〔►see JavaScript: String Methods for RegExp

Note: if you never worked with regexp before, don't worry. regexp is not something you understand the first day. It takes many months of use to be fully comfortable.

Basic Example using RegExp with If

In this example, we want to check if a string contains the substring “oo”.

var xlink = "http://google.com/";

var search_result = xlink.search(/oo/);

if ( search_result !== -1) {
    console.log("found match");
    console.log(search_result); // 8 ← the index of match
} else {
    console.log("not found");
}

Regexp Replace String Example

Regexp can also be used to replace parts of a string that matches a pattern.

// example of RegExp replace
// add a alt attribute to a HTML image tag.

var x1 = '<img src="cat.jpg">';

var x2 = x1.replace(/<img src="([-_\w]+)\.jpg">/, '<img src="$1.jpg" alt="$1">');

console.log(x2); // prints <img src="cat.jpg" alt="cat">

Regexp Example: Get All Captured Patterns

// example of RegExp “match” method, capture groups
// capture the attribute values in a image tag
var xx = '<img class="pict" src="cat.jpg" alt="my cat" width="600" height="400">';

var result = xx.match(/<img class="([^"]+)" src="([^"]+)" alt="([^"]+)" width="([^"]+)" height="([^"]+)">/);

console.log(result[0]); // <img class="pict" src="cat.jpg" alt="my cat" width="600" height="400">
console.log(result[1]); // pict
console.log(result[2]); // cat.jpg
console.log(result[3]); // my cat
console.log(result[4]); // 600
console.log(result[5]); // 400

RegExp Topic

  1. JavaScript: RegExp Tutorial
  2. JavaScript: String Methods for RegExp
  3. JavaScript: RegExp Object
  4. JavaScript: RegExp Constructor
  5. JavaScript: RegExp.prototype
  6. JavaScript: RegExp Syntax

JavaScript Basics

  1. How to Run JavaScript
  2. JavaScript: How to Use Browser's JS Console
  3. JavaScript Tutorial by Example
  4. JavaScript: Data Types
  5. JavaScript: What's True, False?
  6. JavaScript: Operators
  7. JavaScript: Variable
  8. JavaScript: Branch Control: if then else, switch
  9. JavaScript: for while do Loop
  10. JavaScript: Array Basics
  11. JavaScript: Object Basics
  12. JavaScript: RegExp Tutorial
  13. JavaScript: Throw Try Catch Finally
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.