Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Search and replace in large string

    I have a large string, but need to search for a specific character and replace it only if it is between two other items.

    Example
    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You do not say what you want to replace the & symbol by, but:-

    Code:
    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)

  • Users who have thanked Philip M for this post:

    itsallkizza (06-03-2009)

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Philip,

    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.

  • #4
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    That's exactly what Philip went to the trouble of making for you. But if you need it dumbed down further:
    Code:
    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");
    alert(largeString);
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    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.
    Last edited by dprichard; 06-03-2009 at 09:50 PM.

  • #6
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #7
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Quote Originally Posted by dprichard View Post
    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.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

    Code:
    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>.
    Last edited by Philip M; 06-04-2009 at 08:55 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •