December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 63 | Replacing Duplicate Whitespaces with the Same Type |
Imagine that your whitespace removal script is effective for paragraphs and headings, but you come across posts that have redundant tabs before certain list items, like this:
| | My list: |
| | 1. Item one |
| | 2. Item two |
| | 3. Item three |
If you implement the solution in the preceding recipe, the repeated tab characters will be replaced with a single space character, resulting in an inconsistent appearance of the items in the list:
| | My list: |
| | 1. Item one |
| | 2. Item two |
| | 3. Item three |
You need a solution that replaces repeated whitespaces with the same type of whitespace.
Capture the repeated whitespace with a capturing group and reference it with $1:
Read now
Unlock full access