Useful Regex Flags and String Methods

Cristian Cedacero
4 min readNov 9, 2020

--

Regular expressions(regex) can be scary. I remember the first time seeing a regular expression and wondering what on earth was going on. Luckily for us, there’s plenty of resources to help us understand regular expressions. I’ve gone through many of these and I’ll be sharing the regex flags I think can help make life easier.

Writing a regular expression

There are two ways to write regular expressions. You can either create a regex inline(example1) or define your expression separately(example2). I’ve included both ways below. In more complex searches, it’s easier to use example 2.

Example1 - Inline 
string.match(/puppy/)
Example2 - Define separately
const patternToMatch = new RegExp("puppy");
string.match(patternToMatch)

Important Regex Flags and String Methods

Simple regex pattern searching for one word or character

match: Strings have a match property you can use by passing it a regular expression defining the patter you’re trying to match. If you’re using match with no flags like above, match will return an array with the pattern matched, the idex location, and the passed string. If you pass it a global flag like below, it will return an array with the number of instances it matched.

Regex global flag returns all matches

g: The g flag stands for global. You pass this flag to your regex and your pattern will be matched anywhere in the string.

An example of the regex flag with the global and case insensitive flag

i: The i flag stands for case insensitive. This will ensure your regex expression matches both uppercase and lowercase characters.

Regex with brackets matches all characters inside the brackets

/puppy/ || /[puppy]/ : What’s the difference between these two patterns ? If you use the forward slashes only, you’re searching for that exact sequence of characters like above. If you add it inside brackets enclosed by forward slashes, you’re searching for any character from the pattern you’ve passed. You can see a clear example above.

Regex caret key example matching all opposites

^ : The caret key in regex reverses your pattern query. It provides for a simple way to filter out the opposite of your query. If you wanted to match all characters not in the word ‘puppy’ you could use the caret key like above.

Regex to match a number

Matching Numbers : You can match numbers by passing them inside brackets. If you want to match one number, pass that number. If you want to march larger numbers you can get creative by passing a number in curly braces which defines the number of occurrences the pattern must occur in order to be matched, as seen in our example below. A shortcut to match numbers is also adding the \d flag which stands for digit. This is the same as passing [0–9];

Regex matching 2 digit numbers

replace : The string replace property is super useful when you’d like to replace or find differences between two strings. Use it by passing your regex pattern with the character or word to replace and what to replace it with. Below, we’re replacing the word puppy with the word dog. You can use this along with brackets to replace specific characters within a word or number to meet your needs.

Regex to replace a string

test: Strings also have a test property that can be used with a regex to test whether or not your pattern exists within the string. This is similar to match except that it only returns a boolean, unlike match which returns the found match.

Regex to test whether the passed work exists in the string

There’s so much more to regular expressions and I’ll write another article to cover more complex searches but for the time being, I hope you can see how powerful regex can be. I hope to master regex one day, but I hope I was able to teach you a thing or two about regex. Happy learning !

Some Regex Special Characters

A list of common regex special characters

--

--