Jump to content

How do I get rid of the string ' '


tedbyers

Recommended Posts

Here is the context. I have a number of forms that have optional fields, and inthe DB these are represented by empty strings. Here is a very short example of a Javascript function I use to autopopulate a form when a user clicks on a row in a table:

		function getProcessorRow(t) {			var col=t.cellIndex;			var row=t.parentNode.rowIndex;			var procTable = document.getElementById(\"processortable\");			var StrippedString = (procTable.rows[row].cells[1].innerHTML);			StrippedString = StrippedString.replace(/(<([^>]+)>)/ig,\"\");			document.pform.bname.value = StrippedString;			StrippedString = (procTable.rows[row].cells[0].innerHTML);			StrippedString = StrippedString.replace(/(<([^>]+)>)/ig,\"\");			document.pform.pid.value = StrippedString;			StrippedString = (procTable.rows[row].cells[2].innerHTML);			StrippedString = StrippedString.replace(/(<([^>]+)>)/ig,\"\");			document.pform.sname.value = StrippedString;			StrippedString = (procTable.rows[row].cells[3].innerHTML);			StrippedString = StrippedString.replace(/(<([^>]+)>)/ig,\"\");			document.pform.tz.value = StrippedString;			StrippedString = (procTable.rows[row].cells[4].innerHTML);			StrippedString = StrippedString.replace(/(<([^>]+)>)/ig,\"\");			var bgt = StrippedString;			if (bgt == 'Bank') {			   document.pform.bank_type.checked = true;			} else {			   document.pform.gateway_type.checked = true;			}			document.pform.processorsave.value = 'Update Data';		}

What is presently happening is that when this function is executed, and the cell in the table has an empty string (or so it looks to me), The form's corresponding field actually displays the string ' ' when in fact it ought to remain or become (depending on the value it had before the click event) empty. NB: I am using flexigrid, and it puts odd html markup in the table's cells, and that is what the regex applied to StrippedString is designed to remove. But I am at a loss as to how to prevent these auto-population functions from displaying this non-breaking space markup when it ought to display nothing. Please help. Maybe I am missing the obvious, but this is driving me insane. :-( In Perl it wouldn't be an issue (that's where I learned regex, BTW), and maybe I am too tired to see it, but I'd appreciate it if someone would be kind enough to show me my mistake. Thanks Ted

Link to comment
Share on other sites

You'll have to remove the "&nbsp;" manually, you can't remove it using trim or something like that. It's a high-level UTF character, different codes depending on the browser, which just renders as a blank space. The innerHTML property should include the string "&nbsp;", so you'll need to search for that and remove it.

Link to comment
Share on other sites

Thanks, That is much as I'd expected, but then, how do I modify the following statement to do it:

StrippedString = StrippedString.replace(/ /ig,"");

This was one of the first things I'd tried, but it doesn't work. My guess is that the UTF encoding is messing up the regex I tried to use. Can I even do this reliably using regex? NB: This is strictly a client side problem and everything is all ASCII on the server side. I expect I'll have to deal with i18n at some point, but I want to have the core functionality all working before dealing with i18n.

Link to comment
Share on other sites

OK, I looked up non-breaking spaces on Wikipedia, and added the following to my code:

StrippedString = StrippedString.replace(/\U+00A0/ig,"");StrippedString = StrippedString.replace(/\U00A0/ig,"");StrippedString = StrippedString.replace(/0xC2/ig,"");StrippedString = StrippedString.replace(/0xA0/ig,"");

But that was to no avail. There must be something more than just the UTF code for a non-breaking space at work, unless the codes I found for non-breaking spaces are incorrect (in which case, the wiki page needs to be fixed).

Link to comment
Share on other sites

Edit: Never mind, I see that you've tried it.

Edited by Err
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...