Colin McDonnell
Colin McDonnell

Reputation: 981

javascript regex matching periods preceded by spaces/" "

I'm trying to parse a big HTML string so I can find all instances of a period that is preceded by any number of spaces (" ") or non breaking spaces (" "). Then I want to replace all those instances with the spaces stripped out.

So far I have tried:

var ptn = "/( | )+[.]";

and many other variants, but none of them match correctly.

Any ideas? Thanks!

Upvotes: 3

Views: 748

Answers (2)

anubhava
anubhava

Reputation: 786091

Can you try this regex:

var repl = html.replace(/(\s| )+\./g, '.');

Upvotes: 0

Rhyono
Rhyono

Reputation: 2468

What about this? replace(/( | )+(\.)/g, "$2")

The $2 preserves the second match (e.g. the period).

Since we know it's always a period, you could also do the more simplistic:

replace(/( | )+\./g, ".")

Upvotes: 1

Related Questions