tags:

views:

177

answers:

3

Content is

    Hello World.

<a&nbsp;href="#"&nbsp;target=_blank>hello&nbsp;World</a>

How to replace the &nbsp; in html code and keep the other &nbsp; in the text.

+2  A: 

Can you try searching for

(?<=<[^>]*)&nbsp;

and replacing it with a single space?

This looks for &nbsp; inside tags (preceded by a < and possibly other characters except >).

This is extremely brittle, though. For example, it will fail if you have </> symbols in strings/attributes. Better avoid getting those &nbsp; into the wrong locations in the first place.

Tim Pietzcker
RegEx for simple replace? This looks like trying to shot a single fly with nuclear bomb :)
Andrey
@Andrey: It's not a simple replace. You might want to read the last line of the question again. ;-)
Heinzi
Thanks.It works OK.
AlphaWu
A: 

It's simple

youString.Replace("&nbsp;", " ");

String class http://msdn.microsoft.com/en-us/library/system.string.aspx

Replace method http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

Andrey
I think this doesn't solve the "keep the other   in the text." part...
Heinzi
agree, in this case he should use regex.
Andrey
A: 

This will find you all those strips of the text containing &nbsp:

<[^>]+?&nbsp;[^<]+?>

Fropm here you can just do a simple string replaces with the space since Regex will give you the lcoation ofthe match in your text.

Aliostad