TIL: My RegEx Journey, pt2

Continuing with regex stuff where I left off yesterday, although my mind is in a completely different place tonight.

I had originally planned on dragging this out to 3 or 4 blogs, but I have a feeling my focus is going to be shifting tomorrow. I’m sure I’ll be back to them some day, but for now I think I at least have it beat into my head to use regexes when I see a string pop up.

Wildcard Matching

To do a wildcard match on a single character, you can use the . character inside of the query. This will only work for a single character.

For example, if you wanted to key off of run, sun, fun, pun, and nun, you could do the following query:

"This is a lot of fun to run in the sun".match(/.un/g);
// ["run", "sun", "fun"]

Match Single Character with Multiple Possibilities

You can use [] brackets to indicate characters you wish to match against. This could be used if you’re trying to match multiple words that are similar:

"The big bug bag is full".match(/b[iua]g/g);
// ["big", "bug", "bag"]

Character Sets

The same notation as above can be used to note a range of characters to pull from.

Rather than listing the characters out one at a time, they can be listed like the following [a-z] which will cause any value between lower case a and z to be matched.

Match Number and Letters

The same also applies for number strings. If you want to key off of any digit, you can simply put the range of digits that you’re looking for in brackets: [1-5].

This can then be combined with the letter format: [a-z0-9A-Z]

Match Characters Not Specified

To match off of characters that are NOT present in your query, you can use the ^ to denote not to match on the following items. This is also known as a negated character set.

An example would be: [^aeiou].

Match on Characters That Appear One Or More Times

In order to match off of repeated characters, you can use the + symbol to denote that the character may be repeated multiple times, and to include all concurrent instances in the same match.

For example, to key off of multiple a’s in a row, you’d use /a+/g

In this case we’ll only be matching if the character appears at least once.

Match on Characters That Appear Zero Or More Times

This is the same as matching one or more times, only swapping out the + character for a *. This will tell the regex to match off of everything up to and possibly including the character immediately preceding the start.

For example, in /go*/g, a simple g would match, but so would gooooooooo.