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

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -