简体   繁体   中英

Regex replace multiple spaces unless preceded by . or followed by digit

I'm trying to construct a regex replace function that will change all instances of multiple spaces to a single space, unless it is two spaces preceded by a period (.), or multiple spaces followed by a digit ([0-9]).

Example:

//original string
"The dog  jumped  over the fence.   So did the cat    900kg"

//should be
"The dog jumped over the fence.  So did the cat    900kg"

What I have so far:

string.replace(/(?<!\.)  +(?=[^0-9])/g,' ');

This only issue is that this expression only leaves 2 spaces before the digit instead of leaving them all.

(?<.\?)\s\s+(?!\d|\s)

Not a . , one space , one or more additional spaces followed by not a number or another space .

https://regex101.com/r/sfUfRI/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM