JavaScript Regular Expressions..30daysofjavascript => Day 11

Olajide Blessing Niniola
3 min readApr 2, 2021

A regular expression is a set of characters that forms a search pattern, these patterns are used to match character combinations in strings. To break it down, a regular expression is what is used for text search and text replacement operations in JavaScript. There are two expressions that will be globally worked with throughout and they include modifiers and patterns.

Regular Expression Modifiers

Modifiers can be used to perform case-insensitive more global searches

  • i: Perform case-insensitive matching
  • g: Perform a global match (find all matches rather than stopping after the first match)
  • m: Perform multiline matching

Regular Expression Patterns

Brackets are used to find a range of characters:

  • [abc]: Find any of the characters between the brackets, of course, these characters could be within any of the 26 alphabets ie a-z.
  • [0–9]: Find any of the digits between the brackets
  • (x|y): Find any of the alternatives separated with |

The regular expression pattern is quite simple and can be written as follows

/pattern/modifiers;

Using string methods

There are two string methods associated with regular expressions namely:

The search() the method uses an expression to search for a match and returns the position of the match.

The replace() method returns a modified string where the pattern is replaced.

replace() method

The match() method returns an array containing all matches, capturing groups and returning null if no match is found.

The split() method uses regular expression or a fixed string to break a string into an array of substrings. Get a full reference here.

split() method

Regular Expression Object

Aside from strings, JavaScript regular expression also uses objects. These objects have predefined properties and methods.

  • Test(): The test() method searches a string for a pattern(or a character) and returns true or false depending on the result.
  • Exec(): This method searches a string for a pattern(or a character) and returns the found text as an object.

The above are just the very basic when working with regular expressions many tutorial sites have taken their time to give full and detailed references. Click the link below to get a glance.

What is this about?

30daysofjavascript is a series of writing on how I learn to code in JavaScript. These episodes are as simplified as possible and for beginners like me, I hope you find JavaScript less confusing throughout this episode. Thank you as always and see you in the next episode. Check out every episode I have written here.

--

--