PDA

View Full Version : Problem with string replace



denhamd2
03-06-2009, 05:25 PM
Hi,

I have a problem in that I have values coming from a database which contain   instead of a normal space. I'm trying to replace all instances of this with a normal space. They are outputted in my javascript within a new Option function, so I'm not sure how to go about doing a string replace on this. Any help would be much appreciated. Here is my code:



if (chosen4 == "17") {
selbox4.options[selbox4.options.length] = new
Option('Only 34.99 EUR','link1.html');
}

jscheuer1
03-06-2009, 06:06 PM
The way it looks there, it's hard coded, just change it to a regular space.

But to answer your question about a string. Say your string is in a variable named str:


str = str.replace(/\xa0|(\ )/g, ' ');

will replace all nbsp characters as well as all nbsp entities in it with space (hex 20) characters. The net result will be that any run of one or more nbsp's will then be rendered in HTML as a single wrap capable space.

Master_script_maker
03-06-2009, 08:03 PM
John i believe there is a typo in your script:

str = str.replace(/\xa0|(\&npbs;)/g, ' ');
str = str.replace(/\xa0|(\ )/g, ' ');

jscheuer1
03-07-2009, 12:34 AM
John i believe there is a typo in your script:

str = str.replace(/\xa0|(\&npbs;)/g, ' ');
str = str.replace(/\xa0|(\ )/g, ' ');

That's right, shows you what too much multitasking can do.