paulmo Posted January 2, 2015 Share Posted January 2, 2015 Please advise why this only replaces the first instance of "Linux" in the JSON object "result": getJSON('https://www.googleapis.com/freebase/v1/text/en/linus_torvalds').then(function(data) { var change = JSON.stringify(data.result).replace("Linux", "Windex"); for (var i = 0, len = change.length; i < len; i++) { result.innerText = change; }}, The for loop isn't doing anything here...same result if I take it out. Thanks. Link to comment Share on other sites More sharing options...
Ingolme Posted January 2, 2015 Share Posted January 2, 2015 The string replace method only changes the first match found. If you want to change all of them you need a global regular expression. .replace(/Linux/g, "Windex"); The for loop you made is just doing an assignment, it's not actually manipulating the string. Link to comment Share on other sites More sharing options...
paulmo Posted January 2, 2015 Author Share Posted January 2, 2015 Thanks Ingolme. Happy New Year. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now