Jump to content

jQuery & JSON replace all from string


Mudsaf

Recommended Posts

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

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

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

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

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

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

 

 

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

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

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 by Mudsaf
Link to comment
Share on other sites

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 by Mudsaf
Link to comment
Share on other sites

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

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