Home » Tutorials » JavaScript » RegExp


JavaScript RegExp (Regular Expressions) will save your life when you are searching and/or replacing a set characters in a string. It is fundamental for any form validation. RegExp is actually an object. Regular Expressions are common in any language to sift through string values. Usually, RegExp is used to find something inside a string as in some form of validation.

Think about how complex a string truly is. There are so many different characters, combinations and permutations of those characters. What if you wanted to validate that a string contained “I’m awesome”, but the case didn’t really matter and you wanted to know how many times it was used. JavaScript does have a few methods that might help you with this, but eventually the situation might become too difficult for these. We will need to bring in the powerful RegExp to help us out.

Regular expressions are a very advanced topic. It is all right if you don’t completely understand them now, but a reference list of JavaScript’s regular expressions will be very beneficial to you in the future. Try to go through each part relating them back to the original example.


  • i – not case sensitive
  • g – finds all matches not just first match
  • m – multiline matching

RegExp Methods

  • exec() – returns first match if found
  • test() – returns true if found, false it is isn’t found
var example1 = "Meerkats";
var rEPattern1 = /s/gi; // the "s" is what we are searching for
var rEPattern2 = /z/gi; // the "z" is what we are searching for

document.write(rEPattern1.exec(example1) + "<br/>"); // s is found, returns s
document.write(rEPattern2.test(example1) + "<br/>"); // z is not found, returns false


  • [ghi] – returns all characters within the brackets
  • [^ghi] – returns all characters that are not within the brackets
  • [0-9] – returns any digit between 0 and 9
  • [a-z] – returns all lowercase characters between a and z
  • [A-Z] – returns all uppercase characters between A and Z
  • [a-zA-Z] – returns all characters between a and z regardless of case
  • (meerkat|squirrel) – returns any of the alternatives


  • . – single character
  • w – character in word
  • W – non-word character
  • d – digit
  • D – not a digit
  • n – newline character
  • s – white space
  • S – not white space
  • b – Find a match at the beginning/end of a word
  • B – Find a match not at the beginning/end of a word


The “x” is the following list can be replaced by anything, including characters, words, numbers, etc.

  • x+ – string has at least one x
  • x? – string has zero or more occurrences of x
  • x{y} – (y is a number) string has a y number of x’s in sequence
  • x{y,z} – (y and x are numbers) string has a minimum of y x’s in sequence and a maximum of z x’s in sequence
  • x{y,} – (y is a number) string has a minimum of y x’s in sequence
  • x$ – string ends with x
  • ^x – string begins with x
  • ?=x – string is followed by string x
  • ?!x – string is not followed by string x

Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "RegExp". After Hours Programming. Accessed on June 6, 2024. https://www.afterhoursprogramming.com/tutorial/javascript/regexp/.

  • Stewart, Suzy. "RegExp". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/javascript/regexp/. Accessed 6 June, 2024.

  • Stewart, Suzy. RegExp. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/javascript/regexp/.

0 thoughts on “RegExp”

  1. x{y} – (y is a number) string has a y number of x’s in sequence
    x{y,z} – (y and x are numbers)

    Typo? y and z are numbers << I believe would be the correct way.

    Loving this site just thought I would point this out 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.