Skip to content
Advertisement

Replace spaces around hyphens with nbsp

I’m in need of (probably) regexp (or two) to do following:

  • replace every space after any single letter word with  

I found this question, however, if there’s word with apostrophe (like there’s), previous regexp ‘thinks’ that the ‘s’ after the apostrophe is single letter (which is not right). I want/need the space ONLY after single letter, not any other letter (not after words, not after words with apostrophes)

I’d like someone skilled to update the previous answer with this after apostrophe catch.

Advertisement

Answer

Does this meet your expectations?

/(?<=^w) |(?<=sw) /

See it here on Regexr

(?<=sw) is a lookbehind, that checks if there is a whitespace and a word character before the whitespace. (?<=^w) is to cover the case thats the single letter is the first in the string.

w is containing also a _, if this is not OK than you can define your own character class like [a-zA-Z] or use the unicode stuff p{L} together with the modifier u

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement