Recipe 46Referencing a Matched String with the Backreference

Task

Suppose your task is to inspect documents for duplicate words, such as “the the book,” which is a common typographical mistake with texts subject to heavy editing. The task involves looking for repeated words despite capitalization differences like “This this.” It also requires finding instances with varying amounts of whitespace between the words, including tabs and newlines.

You need to come up with a solution that will find all duplicate words and automatically fix them.

Solution

Capture the word with a capturing group, and use a backreference to match the same text matched by the capturing group:

 function​ dupWordRemover(str) {
 

Get Text Processing with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.