substitute two ore more with two or more

From: Robert Wallace (robw_at_sofw.org)
Date: 12/31/03


Date: Wed, 31 Dec 2003 11:31:59 -0500

I want to find two or more spaces and replace them with the same number
found.
I there are three items found, replace them with three other items. four
for four, etc...

If I have:
$x="<img src='pic.gif'> a b c d";
and I want to output:
<img
src='pic.gif'>&nbsp;a&nbsp;&nbsp;b&nbsp;&nbsp;&nbsp;c&nbsp;&nbsp;&nbsp;&nbsp;d

I thought this would work:
$x=~s/ {2,}/&nbsp;/g;
but that replaces all two or more spaces with one &nbsp;

these didn't work
$x=~s/ {2,}/$1/g;
$x=~s/ {2,}/(($1)x3)/g;