News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

JS regexp Q

Started by Curbow 5, September 11, 2004, 07:51:12 AM

Previous topic - Next topic

Curbow 5

i need some help creating a regex which converts all spaces to non-breaking spaces, EXCEPT for spaces within HTML tags! Any help would be greatly appreciated!


// this string:
var str '<span id="test">t e s t</span><br />';
// should become
var str '<span id="test">t&nbsp;e&nbsp;s&nbsp;t</span><br />';

// this converts ALL spaces...
str str.replace(/\s/g'&nbsp;');

Curbow 5

i'm kind of embarrased to ask again, but uhm... anyone? ::)

[Unknown]

Well, this is going to be really hard.... I suggest replacing '  ' with '&nbsp; '.  Meaning, two spaces with a space and an &nbsp;.  Also replace '~(^|<br />)[ ]~' with '$1&nbsp;'.  That should work, no?

-[Unknown]

Curbow 5

thanks! i was afraid it wouldn't be a pretty solution... :(

Advertisement: