View Full Version : Search and replace in large string

06-01-2009, 07:48 PM
I have a large string, but need to search for a specific character and replace it only if it is between two other items.

mystring would be something like this

<parent><child1>1 & 2</child1><child2>1 & 2</child2><child3>1 & 2</child3></parent>

I need to replace the & symbol in child2, but not 1 or 3. I am trying to figure out how to replace just any & signs in child2, but I need to leave them alone in child 1 and 3.

Any help on this would be greatly appreciated. I know I can do a replace(), but just not sure how to limit it to only the one I need

Philip M
06-01-2009, 08:22 PM
You do not say what you want to replace the & symbol by, but:-

var largeString = "<parent><child1>1 & 2</child1><child2>1 & 2</child2><child3>1 & 2</child3></parent>"
largeString = largeString.replace(/(\<child2\>1)(\s\&\s)(2)/,"$1 XX $3");

"Success is the ability to go from one failure to another with no loss of enthusiasm." - Sir Winston Churchill, British politician (1874 - 1965)

06-03-2009, 09:52 PM

Thanks so much for the prompt answer. I guess my example was not the best. The name of the node is actually <description></description>

I have a variable that has a bunch of XML in it and I need to replace any ampersands in the <description></description> node with and. So, if there is an & make it and.

06-03-2009, 10:18 PM
That's exactly what Philip went to the trouble of making for you. But if you need it dumbed down further:

var largeString = "<parent><child1>1 & 2</child1><description>1 & 2</description><child3>1 & 2</child3></parent>"
largeString = largeString.replace(/(\<description\>1)(\s\&\s)(2)/,"$1 and $3");

06-03-2009, 10:47 PM
Wow, ok. I was using child1 and child3 as examples. There is not really a child1 and child3 and the string in <description></description> will not always be the same.

06-03-2009, 11:01 PM
You can replace "description" in the RegExp with a variable of your choice. Tell us how you're determining which xml tags you want to perform the replace in and we can help you. If you have a predefined list you can place them in an array and dump that into Philip's RegExp using ORs.

06-03-2009, 11:08 PM
The company is using some old home made language to write the variables out to the page. They are posting the information to a third party and it has to be a form value with XML in it. The description can be any of the items in their cart so it will be different for each item, but the item description has to be in a <description></description> tag and it has other tags all around it, itemno, itemname, and others. The problem is the description field cannot contain ampersands so if there is one I need to replace it with and. I have no reference for their home made language so I can't do it in that. I have only been using javascript for a few weeks and could easily do this in ASP but javascript replace is a little weird to me. Sorry to sound stupid on the subject.

06-04-2009, 12:20 AM
The description can be any of the items in their cart so it will be different for each item, but the item description has to be in a <description></description> tag and it has other tags all around it, itemno, itemname, and others.

Though DOM parsing would be better for speed reasons if the XML string is huge, the RegExp given above does this already. I'm confused as to what you're JS is still lacking. If you can explain in detail what isn't working it may help solve the problem.

Philip M
06-04-2009, 08:53 AM
This is a good example where the OP does not define or describe his problem properly at the outset, thus leading to frustrating and time-wasting attempts at clarification.

My understanding is that the string within <description> </description> may contain one single ampersand which you want to replace by and.

var largeString = "<parent><child1>1 & 2</child1><description>apples & pears</description><child3>1 & 2</child3></parent>"
largeString = largeString.replace(/(\<description\>.{1,30})(\&)/,"$1AND");
alert (largeString);

The 30 is required to force the replacement here and not the last & in the string. So the ampersand must occur within 30 characters after <description>.