CS133JS Beginning Programming: JavaScript

Regular Expressions

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7. Objects and Object Constructors
3. Conditional Statements: if and switch8. Web Page I/O
4. Repetition: while, do while, and for9. Regular Expressions
5. Midterm Check-Point10. Review

Table of Contents

Introduction

Announcements

For winter 2024

Q and A


 

Regular Expressions

One way to compare strings to see if they match is to use a Regular Expression object, this object is a part of the JavaScript language, and something similar exists in almost every other programming language.

RegExp – Regular expression object. Used for pattern matching in strings. A JavaScript RegExp object can be created two ways:

The real power is in finding partial matches. Regular expressions are a powerful way to find matches for complex patterns in a string.

RegExp Methods

These are the most commonly used methods. For a comprehensive list, see the description of RegExp on MDN.

test

This method will return true when you pass it a string that contains a match for the pattern defined in the RegExp object.

exec

This method will return an array containing just the first matched sub-string. The array also has a number of properties; including the index of the first match in the stringif it finds a match, otherwise it returns null.

This method has a number of other more complex features that you can read about in the MDN documentation for exec.

Matching "wild card" Characters

You might have used * and ? as wildcards in a search before. With RegExp, the syntax is a little different:

Matching at the Beginning, Middle, or End of a String

Anchors are used to indicate that a pattern must be applied at the beginning of a string, the end, or must match the entire string.

Flags

Groups

Quantifiers

Curly braces, { }, specify the number of times a pattern must match:

For example, the pattern /[0-9]{5}/ will match only strings containing 5 digit numbers like: "97405".

 

Escape Characters

Escape character – backslash is an escape character that lets you use a special character, like the dot as a dot, not for pattern matching. For example, check for a period at the end of a string:

let pattern = /\.$/

Metacharacters

Metacharacters are characters with a special meaning. A partial listing is shown below. Notice that the upper-case versions do the inverse of the lower-case versions.

MetacharacterDescription
\wFind a word character (a-z, A-Z, 0-9 and _)
\WFind a non-word character
\dFind a digit
\DFind a non-digit character
\sFind a whitespace1 character
\SFind a non-whitespace character
\bFind a match at either the beginning or end of a word.
\BFinc a match that is not at the beginning or end of a word.

For a complete list, see the W3Schools JavaScript RegExp Reference in the References below.

Here is an example that will only match whole words:

 

Choice (Logical OR)

Pipe character, | to allow choice between patterns

If you want to add a modifer before or after the choice, put the choice inside parenthesis:

 

Examples

 

Resource

Regular Expression Test Page

Try out regular expressions to see how they work with different test strings.

 

References

JavaScript RegExp ReferenceW3Schools

JavaScript Guide: Regular ExpressionsMDN

Regular ExpressionsCh. 9 in Eloquent JavaScript, 3rd Edition, by Marijn Haverbeke, No Starch Press, 2018.


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird, written 2018, updated are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


1 Apace (␣), tab (\t), new line (\n) and the carriage return (\r) are all considered white space.