how do i find a string between 2 delimiters please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil

    how do i find a string between 2 delimiters please

    hello

    I am a trying to find the text between a ? mark and an = sign:
    var theData;
    var begin;
    var thePageNo;
    var beginFrom;
    beginFrom = top.location.hr ef.indexOf("=") ;
    begin = top.location.hr ef.indexOf("?") ;
    if (begin > 0 )
    {
    theData = top.location.hr ef.substring(be gin+1,location. href.length);
    theData = unescape(theDat a);
    ..........
    --
    what I need to do is pass the filename without the + sign and whatever
    follows it.

    So if the string passed is:


    I want to be able retrieve only the string up and including the = sign.
    ie.: http://philippeoget.50megs.com/?IT_S...nformation.htm

    I tried:
    theData = top.location.hr ef.substring(be gin+1,location. href.length-beginFrom);

    but it doesn't like it..

    TIA
    Regards

    Phil
  • kaeli

    #2
    Re: how do i find a string between 2 delimiters please

    In article <f6350b11.04051 20653.62d0804e@ posting.google. com>,
    philippeoget@ya hoo.com enlightened us with...[color=blue]
    > hello
    >
    > I am a trying to find the text between a ? mark and an = sign:
    > var theData;
    > var begin;
    > var thePageNo;
    > var beginFrom;
    > beginFrom = top.location.hr ef.indexOf("=") ;
    > begin = top.location.hr ef.indexOf("?") ;
    > if (begin > 0 )
    > {
    > theData = top.location.hr ef.substring(be gin+1,location. href.length);
    > theData = unescape(theDat a);
    > .........
    >[/color]

    This worked for me. It's extensible so the URL can have more params. It
    assumes the one you want is the first part of the first pair ([0]).

    <html>
    <head>
    <title> New Document </title>
    </head>

    <body>
    <script language="javas cript" type="text/javascript">
    // split the query string into param=val pieces
    var qs = location.search .substr(locatio n.search.indexO f("?")+1);
    var pieces = qs.split("&"); // pieces has all the name/value pairs

    var piece = null;
    if (typeof pieces[0] != "undefined" ) piece = pieces[0].split("="); //
    piece has individual name/values for pieces[0]

    if (typeof piece[0] != "undefined" ) url = self.location.h ref+piece[0];
    alert(url);
    </script>
    </body>
    </html>

    --
    --
    ~kaeli~
    Shotgun wedding: A case of wife or death.



    Comment

    • Philippe Oget

      #3
      Re: how do i find a string between 2 delimiters please



      Regards

      Philippe


      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Shawn Milo

        #4
        Re: how do i find a string between 2 delimiters please

        <snip>[color=blue]
        > So if the string passed is:
        > http://philippeoget.50megs.com/?IT_S...rmation.htm=13
        >
        > I want to be able retrieve only the string up and including the = sign.
        > ie.: http://philippeoget.50megs.com/?IT_S...nformation.htm
        >
        > I tried:
        > theData = top.location.hr ef.substring(be gin+1,location. href.length-beginFrom);
        >
        > but it doesn't like it..
        >
        > TIA
        > Regards
        >
        > Phil[/color]

        The easiest way to do this is with a regular expression.

        I just wrote and tested this:

        var someString =
        'http://philippeoget.50 megs.com/?IT_Study___Inf ormation.htm=13 ';

        document.write( someString + '<br/>');
        someString = someString.repl ace(/^.*\?(.*)=.*$/, '$1');
        document.write( someString + '<br/>');



        Shawn

        Comment

        • Shawn Milo

          #5
          Re: how do i find a string between 2 delimiters please

          Phil,

          Although my last post provided a working solution,
          I realized that there is a flaw in my logic. I was
          assuming that only one name/value pair will ever
          be passed in the querystring. Please see below
          for a correction.

          For regex (regular expression) info, this
          book is the best source:




          Shawn




          var someString =
          'http://philippeoget.50 megs.com/?IT_Study___Inf ormation.htm=13 ';

          //altered to further illustrate regex
          someString =
          'http://philippeoget.50 megs.com/?IT_Study___Inf ormation.htm=13 &lang=en';


          //show original string
          document.write( someString + '<br/>');

          //perform replace
          someString = someString.repl ace(/^.*\?(.*)=.*$/, '$1');

          //show altered string
          document.write( someString + '<br/>');


          //
          someString = '';
          //someString = 'http://www.abc.com?pag e=it_study&lang =en';



          //Note, this regex will work when there are more parameteres
          //in the querystring than just one. The other one will not.
          //Example:

          //The previous regex will return:
          IT_Study___Info rmation.htm=13& lang
          //This one will return: IT_Study___Info rmation.htm

          someString = someString.repl ace(/^.*\?([^=]*)=.*$/, '$1');
          document.write( someString + '<br/>');

          Comment

          • Philippe Oget

            #6
            Re: how do i find a string between 2 delimiters please

            Thank you very much for your help.

            Regards

            Philippe


            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...