Jump to content

Replacing JSON Text


paulmo

Recommended Posts

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

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

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...