I had seen and bookmarked this site ages ago, and decided to give it a run through to improve my regular expression skills http://callumacrae.github.io/regex-tuesday/ So you will probably see some posts of my solutions every now and again for each week.
“make a regex which finds and highlights repeated words in a body of text. ”
- The portion in green, the opening “/” and the ending “/gi” are the delimiters for the expression. gi are pattern modifiers. The “g” makes the regex look for all occurrences instead of stopping at the first match (global match). The “i” makes it case insensitive so it will match “levi” and “LeVi”.
- \b is a word boundary, it will match characters in words. Spaces, numbers, etc… will terminate a word boundary
- [\w’]+ – this tells the regex what to match on. In this case “\w” is a word, so it matches any word. I also include an apostrophe so that is matches words that use it for punctuation. The “+” just means it needs to match 1 or more to be considered a success.
- \s is just a space character. It checks to make sure there is a space between the first word we match and what follows.
- The \1 is a backreference to pull in what has been matched in the first set of parenthesis. This is the magic beans that bring the regex to completion by finding the second match for the first word matched.