3 ms·
Things I love about regex: * The global or g switch. The global switch applies a pattern to all matches in a string, which can be used to return all pattern m
by austincheney 7y ago
Things I love about regex:
* The global or g switch. The global switch applies a pattern to all matches in a string, which can be used to return all pattern matches or apply all pattern changes.
* In JavaScript space like characters are represented by the \s switch. There are many characters classified as white space, so it is very nice to have a grouping for them when performing a search and being able to ignore them all if necessary. \s+ will match a group of consecutive characters that may be any combination of space, new line, carriage return, form feed, and many other characters.
* Partial matches are helpful. Sometimes you know the pattern you want to search for and you need to replace that pattern with a different pattern without harm specific data in the match. In this scenario I perform a replace method on a string and search by regular expression with the a g global switch. For the replacement result I supply a function name. A regular expression match is a string argument in that function. In the function I can manipulate that result to be something else, even using regular expressions, and return the result, which is inserted back into the target string over the regular expression match.