Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing ' ' from strings ??

4,467 views
Skip to first unread message

Mel Smith

unread,
Jan 15, 2014, 5:23:56 PM1/15/14
to
Hi:

I'm probably having a senior moment, but with IE8 as a test bed, I
cannot seem to replace ' ' with an ordinary space character.

e.g.,

Let's say my input text string is:

var mytext = "  " + "Hello" + " " + "World" +
" "

Using IE8:

var newtext = mytext.replace(" "," ") ;

The above statement*works* in Chrome (i.e., it replaces ' ' with an
'ordinary' space character

But, in IE8 , these ' ' sequences are *not* replaced -- just
ignored ??

I need a 'kick-in-the-butt' please -- perhaps a real regex string that would
work in all cases ?

Thank you.

-Mel Smith


Evertjan.

unread,
Jan 15, 2014, 5:35:50 PM1/15/14
to
"Mel Smith" <med_cuto...@aol.com> wrote on 15 jan 2014 in
comp.lang.javascript:

> Hi:
>
> I'm probably having a senior moment, but with IE8 as a test bed, I
> cannot seem to replace '&nbsp;' with an ordinary space character.
>
> e.g.,
>
> Let's say my input text string is:
>
> var mytext = "&nbsp;&nbsp;" + "Hello" + "&nbsp;" + "World" +
> "&nbsp;"
>
> Using IE8:
>
> var newtext = mytext.replace("&nbsp;"," ") ;
>
> The above statement*works* in Chrome (i.e., it replaces '&nbsp;'
> with an 'ordinary' space character

No, only the first one.

>
> But, in IE8 , these '&nbsp;' sequences are *not* replaced -- just
> ignored ??

IE11 does the same as chrome, are you sure about IE8?

> I need a 'kick-in-the-butt' please -- perhaps a real regex string that
> would work in all cases ?

Better always use regex with replace(),
and use the global switch if necessary:

var newtext = mytext.replace(/&nbsp;/g,' ') ;




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Ben Bacarisse

unread,
Jan 15, 2014, 6:44:23 PM1/15/14
to
"Evertjan." <exxjxw.h...@inter.nl.net> writes:

> "Mel Smith" <med_cuto...@aol.com> wrote on 15 jan 2014 in
> comp.lang.javascript:
<snip>
>> Let's say my input text string is:
>>
>> var mytext = "&nbsp;&nbsp;" + "Hello" + "&nbsp;" + "World" +
>> "&nbsp;"
>>
>> Using IE8:
>>
>> var newtext = mytext.replace("&nbsp;"," ") ;
>>
>> The above statement*works* in Chrome (i.e., it replaces '&nbsp;'
>> with an 'ordinary' space character
>
> No, only the first one.
>
>>
>> But, in IE8 , these '&nbsp;' sequences are *not* replaced -- just
>> ignored ??
>
> IE11 does the same as chrome, are you sure about IE8?

On my IE8 VM, the code works as expected.

<snip>
--
Ben.

Mel Smith

unread,
Jan 15, 2014, 7:07:24 PM1/15/14
to
> On my IE8 VM, the code works as expected.
>
> <snip>
> --
> Ben.

Ben & Evertjan:

I'll go back to my 'real' code and take another look. I probably (as
usual) did something wrong --- *but* Chrome worked perfectly ??

Thanks,

-Mel


Mel Smith

unread,
Jan 16, 2014, 11:08:38 AM1/16/14
to
Hi All:

My IE8 still won't change the nbsp; coding to spaces altho Chrome does
so.

Below is my actual latest test code

and here is the definition of my alltrim() function

function alltrim(str) {
return str.replace(/^\s+|\s+$/g,"") ;
}

Thanks

-Mel Smith


********* latest test code *************
option = el.options[nindex] ; // text string with multiple 'nbsp;' in it
textstr = option.text ;
textstr = textstr.replace(/nbsp;/g," ") ; // change nbsp; into space
globally

// get the clubname first
npos1 = textstr.search("Tees") ;
clubname = textstr.substring(0,npos1) ;
alert("a. Code at start= " + clubname.charCodeAt(0)+" Code at end=
"+clubname.charCodeAt(clubname.length-1)) ;
clubname = alltrim(clubname) ; // trim spaces off of the ends of the
clubname
alert("b. Code at start= " + clubname.charCodeAt(0)+" Code at end=
"+clubname.charCodeAt(clubname.length-1)) ;
// in both alerts above the chars at the start and end are Unicode 160
(unbreakable space) for IE8

alert("clubname=**"+clubname+"** Len = "+clubname.length) ;
***********************************






Ben Bacarisse

unread,
Jan 16, 2014, 11:18:44 AM1/16/14
to
"Mel Smith" <med_cuto...@aol.com> writes:

> ********* latest test code *************
> option = el.options[nindex] ; // text string with multiple 'nbsp;' in it
> textstr = option.text ;
> textstr = textstr.replace(/nbsp;/g," ") ; // change nbsp; into space
> globally

"nbsp;" is not the same as "&nbsp;".

> // get the clubname first
> npos1 = textstr.search("Tees") ;
> clubname = textstr.substring(0,npos1) ;
> alert("a. Code at start= " + clubname.charCodeAt(0)+" Code at end=
> "+clubname.charCodeAt(clubname.length-1)) ;
> clubname = alltrim(clubname) ; // trim spaces off of the ends of the
> clubname
> alert("b. Code at start= " + clubname.charCodeAt(0)+" Code at end=
> "+clubname.charCodeAt(clubname.length-1)) ;
> // in both alerts above the chars at the start and end are Unicode 160
> (unbreakable space) for IE8
>
> alert("clubname=**"+clubname+"** Len = "+clubname.length) ;
> ***********************************

--
Ben.

Mel Smith

unread,
Jan 16, 2014, 12:23:42 PM1/16/14
to
Ben said:
>
> "nbsp;" is not the same as "&nbsp;".


Whoops !!!

I'll slow dow and try again :(

-Mel


Mel Smith

unread,
Jan 16, 2014, 12:35:48 PM1/16/14
to
Hi:

The corrected real code is below.

Unfortunately, the result id the same: &nbsp; is replaced by " " for
Chrome but *not* for IE8


********************
option = el.options[nindex] ; // text string with multiple 'nbsp;' in it
textstr = option.text ;
textstr = textstr.replace(/&nbsp;/g," ") ; // change '&nbsp;' into space
globally

// get the clubname first
npos1 = textstr.search("Tees") ;
clubname = textstr.substring(0,npos1) ;
alert("a. Code at start= " + clubname.charCodeAt(0)+" Code at end=
"+clubname.charCodeAt(clubname.length-1)) ;
clubname = alltrim(clubname) ; // trim spaces off of the ends of the
clubname
alert("b. Code at start= " + clubname.charCodeAt(0)+" Code at end=
"+clubname.charCodeAt(clubname.length-1)) ;
// in both alerts above the chars at the start and end are Unicode 160
(unbreakable space) in IE8

Martin Honnen

unread,
Jan 16, 2014, 12:58:34 PM1/16/14
to
Mel Smith wrote:

> Unfortunately, the result id the same: &nbsp; is replaced by " " for
> Chrome but *not* for IE8
>
>
> ********************
> option = el.options[nindex] ; // text string with multiple 'nbsp;' in it
> textstr = option.text ;
> textstr = textstr.replace(/&nbsp;/g," ") ; // change '&nbsp;' into space
> globally

Is that script code you have inline inside of an HTML document? Or why
do you use an HTML entity reference?
Have you tried
textstr.replace(/\u00A0/g, ' ')
?



Ben Bacarisse

unread,
Jan 16, 2014, 1:01:23 PM1/16/14
to
Then the replace won't work. /&nbsp;/ does not match U+00A0 (160
decimal), it matches an ampersand followed by an 'n' etc. You can
replace both by writing:

textstr.replace(/&nbsp;|\u00a0/g," ")

> alert("clubname=**"+clubname+"** Len = "+clubname.length) ;

--
Ben.

Mel Smith

unread,
Jan 16, 2014, 1:05:59 PM1/16/14
to
Martin said:
> Is that script code you have inline inside of an HTML document? Or why do
> you use an HTML entity reference?

Yes, it is in a javascript function in an html doc that is getting the
text string from a option of a <select> tag -- but maybe I don't undestand
what you're asking ??

> Have you tried
> textstr.replace(/\u00A0/g, ' ')
> ?

No, but I'll give it a try.

Thanks Martin.

-Mel


Mel Smith

unread,
Jan 16, 2014, 1:10:19 PM1/16/14
to
Hi Ben:

In just tried your /\u00A0/g code and it works perfectly.

Thanks !

-Mel Smith


Denis McMahon

unread,
Jan 16, 2014, 2:17:00 PM1/16/14
to
On Thu, 16 Jan 2014 10:35:48 -0700, Mel Smith wrote:

> textstr = option.text ;

option.text may not be the same as option.innerHTML, especially if the
html contains entity codes. I suspect the issue is that the html entity
codes have been translated into some other encoding scheme (ie other than
html entity encoding) in the text string that you're getting back as the
value of option.text.

--
Denis McMahon, denismf...@gmail.com

Mel Smith

unread,
Jan 16, 2014, 3:37:41 PM1/16/14
to
Denis said:
>> textstr = option.text ;
>
> option.text may not be the same as option.innerHTML, especially if the
> html contains entity codes. I suspect the issue is that the html entity
> codes have been translated into some other encoding scheme (ie other than
> html entity encoding) in the text string that you're getting back as the
> value of option.text.

Hi Denis:

I ave simple text (with the inclusion of &nbsp; codes, and MArtin's
solution resolves everything nicely. IE* and Chrome react exactly the same.

I tried 'innerHTML' for a few minutes, and came up with the long string
of chars interspersed with &nbsp;

So, back to Martin's solution.

Thanks to all !

-Mel


0 new messages