Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript/jQuery removing character 160 from a node's text() value - Regex

$('#customerAddress').text().replace(/\xA0/,"").replace(/\s+/," ");

Going after the value in a span (id=customerAddress) and I'd like to reduce all sections of whitespace to a single whitespace. The /\s+/ whould work except this app gets some character 160's between street address and state/zip What is a better way to write this? this does not currently work.

UPDATE: I have figured out that

$('.customerAddress').text().replace(/\s+/g," ");

clears the 160s and the spaces. But how would I write a regex to just go after the 160s?

$('.customerAddress').text().replace(String.fromCharCode(160)," ");

didn't even work.

Note: I'm testing in Firefox / Firebug

like image 475
BuddyJoe Avatar asked Apr 15 '10 16:04

BuddyJoe


1 Answers

Regarding just replacing char 160, you forgot to make a global regex, so you are only replacing the first match. Try this:

$('.customerAddress').text()
    .replace(new RegExp(String.fromCharCode(160),"g")," ");

Or even simpler, use your Hex example in your question with the global flag

$('.customerAddress').text().replace(/\xA0/g," ");
like image 116
Mark Porter Avatar answered Sep 20 '22 15:09

Mark Porter