Web Analytics Made Easy -
StatCounter How to replace &word= with | within a function? - CodingForum


No announcement yet.

How to replace &word= with | within a function?

  • Filter
  • Time
  • Show
Clear All
new posts

  • How to replace &word= with | within a function?

    I'd like to replace a series of words from a URL string (i.e. &price=) into a non-alpha character such as the vertical bar. I don't know enough about regExp to write the code without getting "invalid character in regExp"

    Can anyone help? I have four words to replace.

    Thanks in advance.

  • #2
    A regular expression is usually the first parameter in the replace method. The following regular expression will help:



    • #3

      Thank you for your response. I'd like to be more selective on the change -- only four particular &strings=, not all of them. I'm trying to concat a product into: code=stamp|price|qty|cost within a function so they can be put into keyPairs and processed as one set, a la sendmail's routine.

      Here's a sample of the string:

      I imagine the logical thing would be to do this from within HTML before SUBMIT'g, but I can't for the life of me figure out how to recapture and use the qty and cost field after it is called upon to calculate price*qty


      • #4
        var values = location.search.replace(/[&\?][^=]+=([^&]+)/g, '$1,').split(',');

        var code = values[0] + '=' + values.slice(1).join('|');

        That puts the example string you gave into:


        That what you wanted?


        • #5

          Method of building the query/search string is FORM GET


          Yes, that is the result I want. I am not however, sure whether the code you supplied will do all occurences of &word=, or only the four that I want. I'm having trouble interpreting the code you supplied I have 60+ occurrences of this string being passed, and other BillTo/ShipTo info tagged on the end. I'd like to keep the function as generic as possible to use elsewhere, so is it possible to just replace specific words?

          Thank you both for your time in responding.


          • #6
            My code will remember all values after an equal sign, then use the first one as the equal one, then join the other's with "|".

            It should work with any number of appended values.


            • #7
              I'm trying to perform the transformation on the receiving page, but because I don't have a unique name for each value, I get 60+ strings of code=, followed by 60+ strings of stamp= and 60 price=, and so on -- but not together, nor related to each other. I'm trying to concat them so I end up with a unique name for each item ordered. Here's the string:


              but there are 60+ of them, plus BillTo and ShipTo info appended on the end of this string, which is ok as is, so I don't want those &word='s to be changed to |.

              So what jkd's code does is give me a unique identifier: AD108=ArtDecoFan|6.25|1|6.25 -- which is what I want.

              Maybe there's a better way, but it's gone missin' on me.


              • #8
                Dave, again thanks for all your help and responding, but I don't think I have a solution.

                jkd's example results is what I want to produce for some 60 or more strings. But if I understand his code correctly, it's going to process all occurrences of &word= into that format. I need a way to just replace four identical words each time it occurs in the whole string. So

                &stamp= changes to |
                &price= changes to |
                &qty= changes to |
                &cost= changes to |

                So this string, and all like it: &code=AD-108&stamp=ArtDecoFan&price=6.25&qty=1&cost=6.25

                become this string: AD108=ArtDecoFan|6.25|1|6.25

                but leaves: &Billto=joejones&Shipto=joanjones


                Sorry this is so confusing.


                • #9
                  In that case, I think you'd want something like:

                  var results = location.search.replace(/\?code=([^&]+)&stamp=([^&]+)&price=([^&]+)&qty=([^&]+)&cost=([^&]+)(.*)$/, '$1=$2|$3|$4|$5$6');



                  • #10
                    jkd + Dave

                    Thank you both for your time in responding; now I'll just have to figure out how to incorporate it into my function routine.

                    I appreciate your being here for us all to learn.