JavaScript replace not removing the whitespace -
i trying replace space in postcode's. have tested replace()
in console , works, not work stepping through in debug chrome.
is there way work?
$('#search_postcode').on('click', function (e) { e.preventdefault(); var getpostcode = $('#txt_search').val(); var postcode = {}; if (getpostcode !== "") { var str = getpostcode; str.replace(/\s+/g,''); if (str.length === 7) { postcode.outward = str.substr(0, 4); postcode.inward = str.substr(4, 3); } if (str.length === 6) { postcode.outward = str.substr(0, 3); postcode.inward = str.substr(3, 3); } console.log(postcode);
it comes space in str
replace
returns new string, doesn't change passed 1 (strings immutable in javascript).
use
str = str.replace(/\s+/g,'');
Comments
Post a Comment