Mudsaf Posted August 9, 2013 Share Posted August 9, 2013 Hello, i'm wondering how to replace all data from content not just the amount $.each function gives. i = 0;$.each(<JSON CODE>, function() {data = data.replace(<REPLACE CODE>,<REPLACE CODE>);i++;}); Example now the result from each is 2 and will replace only 2 items, but if there is more items on data how that works? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 The each method will loop through an array or the properties of an object. If it loops twice then either you have an array with 2 elements or an object with 2 properties. I'm not sure what your question is. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 Example my data would be like 500 characters long and might contain 10x strings i want to replace, without me knowing the amount how many strings there is & have to be replaced. <script>var string = "THIS CAN BE REALLY LONG STRING :I: :I: asdas :I:"; //IDK HOW MANY :I: strings there would be//Replace them all</script> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 You can do a global replace with a regular expression in String.replace. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 You can do a global replace with a regular expression in String.replace. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace data = data.replace(/<myjson>/g,<mystring>); //Didin't work. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 That's right, that's not the correct way to use String.replace. Look at the documentation again. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 That's right, that's not the correct way to use String.replace. Look at the documentation again. var str = "Twas the night before Xmas...";var newstr = str.replace(/xmas/i, "Christmas");print(newstr); //Returns Twas the night before Christmas... Isn't it similar to mine? NEW var data = data.replace(/<myjson>/g,<mystring>); //Didin't work. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 It's similar, yeah. Are you trying to replace the string "<myjson>" with something else? Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 It's similar, yeah. Are you trying to replace the string "<myjson>" with something else? Yes, json array for example my_list.listIndex[0].item Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 So you're trying to replace the entire string, not searching for a substring inside of it. If you're replacing the entire string then you don't need to use replace, you just overwrite the variable. But you're not really trying to replace the entire string, are you? Show the actual code you're using instead of replacing the values with tokens like <myjson>. The values are the important part, there's no reason to remove those. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 Well the values are not actually important. data = data.replace(smiley_list.smileyList[i].smileyCode,"<img class='" + smiley_list.smileyList[i].smileyCode + "' class='sbSmiley' style='vertical-align:middle;' src='script/shoutbox/smileys/" + smiley_list.smileyList[i].smileyURL + "'>"); So bascially i want to replace all codes where is smiley_list.smileyList.smileyCode with src smiley_list.smileyList.smileyCode Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 Well the values are not actually important. Why would you say that? The values are the important part. You have the general syntax right, but the values are what you are telling it to actually do. The code you posted will work, but it's only going to make one replacement. If you want to replace all then the first argument (the value) needs to be a regular expression with the g flag instead of a string. I'm assuming that the smileyCode parameter contains a string and not a regular expression object. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 Yea, how i can turn the string to expression? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 Just create a RegExp object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp Note the table of special characters, if those characters are in the string you might have to escape them. This is why the value is necessary though, when you write <myjson> I can't tell if you're using a regular expression or a string or what. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 (edited) Just create a RegExp object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp Note the table of special characters, if those characters are in the string you might have to escape them. This is why the value is necessary though, when you write <myjson> I can't tell if you're using a regular expression or a string or what. var smiley = new RegExp(smiley_list.smileyList[i].smileyCode, "g"); var data = data.replace(smiley,"<img class='" + smiley_list.smileyList[i].smileyCode + "' class='sbSmiley' style='vertical-align:middle;' src='script/shoutbox/smileys/" + smiley_list.smileyList[i].smileyURL + "'>");Uncaught TypeError: Cannot call method 'replace' of undefined Edited August 9, 2013 by Mudsaf Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 Sounds like data is undefined, like the error says. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 (edited) Alright i totally messed the page, now basically everything = smileys (70%) My JSON {"smileyList": [{ "smileyCode":"[SMILEN]" , "smileyURL":"SM0-001.png"}, { "smileyCode":"[SMILEC]" , "smileyURL":"SM0-002.png"} ]} My JavaScript (JSON stored to global variable named smiley_list Javascript i = 0; $.each(smiley_list.smileyList, function() { smiley = new RegExp(smiley_list.smileyList[i].smileyCode, "g"); data = data.replace(smiley,"<img class='sbSmiley' style='vertical-align:middle;' src='script/shoutbox/smileys/" + smiley_list.smileyList[i].smileyURL + "'>"); i++; }); $("#shoutbox_content").html(data); } Seems like its replacing every s with the image? dafuq Edited August 9, 2013 by Mudsaf Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 In a regular expression, square brackets mean a character class. The regular expression /[sMILEN]/ means to match any S, M, I, L, etc. You need to escape the brackets: /[sMILEN]/ I think technically you only need to escape the opening bracket. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 Well how i can turn easily [sMILEN] to /[sMILEN]/ ? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 You can use String.replace to replace it in Javascript, or you can replace it in PHP before sending it to Javascript. Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 smiley = smiley_list.smileyList[i].smileyCode;smiley = smiley.replace("]","]");Returns [SMILEN] Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 It looks like you need to use ]. 1 Link to comment Share on other sites More sharing options...
Mudsaf Posted August 9, 2013 Author Share Posted August 9, 2013 It looks like you need to use ]. Finally it works, thanks a thousand times :> 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