Regex: The Basics
As I continue my learning through algorithms, I start to recognize how some solutions are better for certain problems. Recently I was working my way through Codewars.com and came across this problem.
When I first read it, I thought this should be easy enough. Since there are several different test cases, I would have to pass in an argument to the function. I began to think okay but how do I even split up the numbers to insert parentheses and dashes? I gave myself about 10 mins before I gave up because I really had no idea. I went to Stack Overflow, a newbie developers best friend(at least mine anyway), and found a solution and it worked perfectly but I wanted to know why it worked. The solution had syntaxes that I don’t recall seeing before so I set out understand them.
Regular Expressions:
In Javascript, are patterns used to match character combinations in strings, they are also objects. These patterns are used with the exec()
and test()
methods of RegExp
, and with the match()
, matchAll()
, replace()
, replaceAll()
, search()
, and split()
methods of String
. I realized when it comes to string, using RegEx is method that can be very efficient when it comes to strings. How you put these expressions together determines your result.
/
—
The forward slash defines expressions, the characters between the slashes is what’s being searched.
Flags —
RegEx has several flag options that allow for functionality such a global search which is defined as g
, it finds all the specified characters anywhere in the string. Case insensitive - i
for case sensitive search, such as a capital letter. multi-line search - m
, a boolean and is true if the "m
" flag was used; otherwise false and a few others.
Plus sign + —
This expression matches one of more of the character preceding it, it matches as many of that character that is found.
? —
This expression will match one or more of each character that includes the preceding character.
The asterisk * —
This expression is a combination of + and ?
, matches 0 or more.
The period /./ —
This expression matches any string that includes characters.
What if you want match all the periods? Using the backslash character allows you to search for all the periods
w —
Matches all alphabet characters.
d —
Matches any digit character 0–9.
s —
Matches any whitespace character; spaces,tabs & line breaks.
{} —
Allows you to specify the amount of characters you’d like to find.
[] —
Matches any character within brackets.
() —
Allows for grouping.
^ —
Matches the beginning of a line of the whole statement.
$ —
Matches the end of the string or line if ‘m’ flag is enabled.
There’s much more regular expressions but I’m only touching on the ones that I used to solve the problem above.
The replace()
method returns a new string with some or all matches of a pattern
replaced by a replacement
. The pattern
can be a string or a RegExp
, and the replacement
can be a string or a function to be called for each match. If pattern
is a string, only the first occurrence will be replaced. The match()
method retrieves the result of matching a string against a regular expression.
At first I thought Regex was scary like I thought about everything else when it comes to programming but once I took the time to really understand, I can see how it’s an efficient solution depending on the problem. I hope this was helpful to someone, as always happy coding !!