Javascript Regular Expressions (regexp)

Javascript regular expressions (regexp) are very useful methods for string manipulation — match, replace, search and split.  I also find them pretty confusing.  In the previous post on the Javascript anagram problem, I used the following regexp to remove all punctuation, etc. from a string: str.replace(/[^\w]/g,).  What does this actually mean?  It’s hard to decipher a pattern just from looking at it.

First off, the search pattern goes between two forward slashes.

const brian = "Brian Hafner Tech Blog" // 0 -- returns index 0 (the first occurence of Brian) // -1 -- returns false as it is case sensitive

Square brackets contain a character set.  For example, [abcde].  This can also be written as [a-e].[abcde]/) // 3 - returns index three which is the "a" in Brian.  
   // The first letter it comes across that matches.

\W matches any letters or numbers.  \w matches punctuation etc.  The ^ sign negates the pattern.  So [^\w] is the same as [\W].  After the final forward slash, “g” means to do a global search.

So with str.replace(/[^\w]/g,”), I am using the replace method to search my str variable for any characters that are not numbers or letters and replace it with an empty string (the two single quotes with no space following the g,” ).

MDN guide to regular expressions:

Leave a Reply

Your email address will not be published. Required fields are marked *