get characters before and after "token"?

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

    get characters before and after "token"?

    I am kind of new to javascript, I am not sure if there is direct support
    for "tokens" in the sense that they are characters that mark places in a
    string.

    For example if I had

    var mystring = "12345:abcd e";

    and I wanted to consider the colon ":" in the middle a token, how would I
    define a new variable that is everything that comes before it (12345)? How
    about for after it (abcde)?

    I am sorry if this is a stupid noob question.

  • d d

    #2
    Re: get characters before and after "token&quo t;?

    Alan wrote:
    I am kind of new to javascript, I am not sure if there is direct support
    for "tokens" in the sense that they are characters that mark places in a
    string.
    >
    For example if I had
    >
    var mystring = "12345:abcd e";
    >
    and I wanted to consider the colon ":" in the middle a token, how would I
    define a new variable that is everything that comes before it (12345)? How
    about for after it (abcde)?
    >
    I am sorry if this is a stupid noob question.
    If you knew for a fact that there was a : in the
    string then you could do this:

    alert(mystring. substring(0,mys tring.indexOf(" :"))); //12345
    alert(mystring. substring(mystr ing.indexOf(":" )+1)); //abcde

    or you could split it into an array:

    var newarray = mystring.split( ":");
    alert(newarray[0]); //12345
    alert(newarray[1]); //abcde

    ~dd

    Comment

    • Alan

      #3
      Re: get characters before and after "token&quo t;?

      Hi,

      Thanks very much that works a treat. I have a follow up question now.
      Sort of the inverse.

      If I have this:

      var mystring = "For $100 more (optional) we can upgrade you ($150.00)";

      How can I extract the number 150.00?

      In this case and the others I need to work with the numerical value is
      ALWAYS between the two characters '($' and the single ')' and that part
      of the string is ALWAYS at the end. ($150.00) or whatever it is is
      always the last characters in the string if that helps.

      I have been trying various things with substring and indexof and
      lastindex of but I can't quite figure it out.



      d d <no_spam@please .netwrote in
      news:f60ucp$26k $01$1@news.t-online.com:
      Alan wrote:
      >I am kind of new to javascript, I am not sure if there is direct
      >support for "tokens" in the sense that they are characters that mark
      >places in a string.
      >>
      >For example if I had
      >>
      >var mystring = "12345:abcd e";
      >>
      >and I wanted to consider the colon ":" in the middle a token, how
      >would I define a new variable that is everything that comes before it
      >(12345)? How about for after it (abcde)?
      >>
      >I am sorry if this is a stupid noob question.
      >
      If you knew for a fact that there was a : in the
      string then you could do this:
      >
      alert(mystring. substring(0,mys tring.indexOf(" :"))); //12345
      alert(mystring. substring(mystr ing.indexOf(":" )+1)); //abcde
      >
      or you could split it into an array:
      >
      var newarray = mystring.split( ":");
      alert(newarray[0]); //12345
      alert(newarray[1]); //abcde
      >
      ~dd
      >

      Comment

      • d d

        #4
        Re: get characters before and after &quot;token&quo t;?

        Alan wrote:
        If I have this:
        var mystring = "For $100 more (optional) we can upgrade you ($150.00)";
        How can I extract the number 150.00?
        In this case and the others I need to work with the numerical value is
        ALWAYS between the two characters '($' and the single ')' and that part
        of the string is ALWAYS at the end. ($150.00) or whatever it is is
        always the last characters in the string if that helps.
        I have been trying various things with substring and indexof and
        lastindex of but I can't quite figure it out.
        This should work:

        var mystring="For $100 (optional) blah ($150.00)";
        var start=mystring. lastIndexOf("$" )+1;
        var end=mystring.la stIndexOf(")");
        var val=mystring.su bstring(start,e nd);


        ~dd

        Comment

        • Evertjan.

          #5
          Re: get characters before and after &quot;token&quo t;?

          d d wrote on 30 jun 2007 in comp.lang.javas cript:
          Alan wrote:
          >If I have this:
          >var mystring = "For $100 more (optional) we can upgrade you ($150.00)";
          >How can I extract the number 150.00?
          >In this case and the others I need to work with the numerical value is
          >ALWAYS between the two characters '($' and the single ')' and that part
          >of the string is ALWAYS at the end. ($150.00) or whatever it is is
          >always the last characters in the string if that helps.
          >I have been trying various things with substring and indexof and
          >lastindex of but I can't quite figure it out.
          >
          This should work:
          >
          var mystring="For $100 (optional) blah ($150.00)";
          var start=mystring. lastIndexOf("$" )+1;
          var end=mystring.la stIndexOf(")");
          var val=mystring.su bstring(start,e nd);
          Regex:

          var myString = 'For $100 (optional) blah ($150.00)';
          var result = myString.replac e(/^.*?\(\$(.*)?\) $/,'$1');

          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)

          Comment

          • d d

            #6
            Re: get characters before and after &quot;token&quo t;?

            Evertjan. wrote:
            d d wrote on 30 jun 2007 in comp.lang.javas cript:
            >var mystring="For $100 (optional) blah ($150.00)";
            >var start=mystring. lastIndexOf("$" )+1;
            >var end=mystring.la stIndexOf(")");
            >var val=mystring.su bstring(start,e nd);
            Regex:
            var myString = 'For $100 (optional) blah ($150.00)';
            var result = myString.replac e(/^.*?\(\$(.*)?\) $/,'$1');
            Cool!! I always try to avoid using Regular expressions.
            Mostly because (a) they're impossible for humans to
            understand, and (2) I'm a human ;-)

            If you're an experienced regexp'er then they're cool.
            But don't expect anyone else to understand them. Of
            course that in itself can be good for job security ;-)

            ~dd

            Comment

            • Evertjan.

              #7
              Re: get characters before and after &quot;token&quo t;?

              d d wrote on 30 jun 2007 in comp.lang.javas cript:
              Evertjan. wrote:
              >d d wrote on 30 jun 2007 in comp.lang.javas cript:
              >>var mystring="For $100 (optional) blah ($150.00)";
              >>var start=mystring. lastIndexOf("$" )+1;
              >>var end=mystring.la stIndexOf(")");
              >>var val=mystring.su bstring(start,e nd);
              >Regex:
              >var myString = 'For $100 (optional) blah ($150.00)';
              >var result = myString.replac e(/^.*?\(\$(.*)?\) $/,'$1');
              >
              Cool!! I always try to avoid using Regular expressions.
              Mostly because (a) they're impossible for humans to
              understand, and (2) I'm a human ;-)
              >
              If you're an experienced regexp'er then they're cool.
              But don't expect anyone else to understand them. Of
              course that in itself can be good for job security ;-)
              Do you mean I should ask money for it?

              Regex is very powerfull, fits nicely in javascipt
              and can be learned in 2 nights.

              I would advice you to spend the time.

              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • d d

                #8
                Re: get characters before and after &quot;token&quo t;?

                Evertjan. wrote:
                Regex is very powerfull, fits nicely in javascipt
                and can be learned in 2 nights.
                I would advice you to spend the time.
                Oh yeah, I've done some regexp stuff. I made an
                Ant+XML script that I use to minimize/obfuscate
                some JS files. But in that case the program to
                do that job without regexp would have been a
                nightmare. I needed a list of the commands to do
                it though :-)

                ~dd

                Comment

                • ron.h.hall@gmail.com

                  #9
                  Re: get characters before and after &quot;token&quo t;?

                  On Jun 30, 8:41 am, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
                  d d wrote on 30 jun 2007 in comp.lang.javas cript:
                  >
                  >
                  >
                  Evertjan. wrote:
                  d d wrote on 30 jun 2007 in comp.lang.javas cript:
                  >var mystring="For $100 (optional) blah ($150.00)";
                  >var start=mystring. lastIndexOf("$" )+1;
                  >var end=mystring.la stIndexOf(")");
                  >var val=mystring.su bstring(start,e nd);
                  Regex:
                  var myString = 'For $100 (optional) blah ($150.00)';
                  var result = myString.replac e(/^.*?\(\$(.*)?\) $/,'$1');
                  >
                  Cool!! I always try to avoid using Regular expressions.
                  Mostly because (a) they're impossible for humans to
                  understand, and (2) I'm a human ;-)
                  >
                  If you're an experienced regexp'er then they're cool.
                  But don't expect anyone else to understand them. Of
                  course that in itself can be good for job security ;-)
                  >
                  Do you mean I should ask money for it?
                  >
                  Regex is very powerfull, fits nicely in javascipt
                  and can be learned in 2 nights.
                  >
                  I would advice you to spend the time.
                  >
                  That's good advice. However, "2 nights" may not suffice.

                  var result = myString.replac e(/^.*?\(\$(.*)?\) $/,'$1');

                  will produce an incorrect result, for example, with:

                  var myString = 'For $100 (optional) ($300.00) blah ($150.00)';

                  There are a number of things wrong in the construction of the regular
                  expression you have proposed, but in particular "?" seems to be
                  misused or misplaced, or both.

                  Great care is always required in composing regular expressions, and
                  even those with a high level of competence in the area need to
                  carefully test that they produce the desired result. They are
                  extremely useful, but they are in my experience, not at all easy to
                  master and use effectively.

                  var m;
                  if ( m = myString.match( /^.*\(\$(\d+\.\d {2})\)$/ ) ) {
                  alert( "Matched: " + m[1] );
                  }

                  may serve better.

                  --
                  ../rh




                  Comment

                  • ron.h.hall@gmail.com

                    #10
                    Re: get characters before and after &quot;token&quo t;?

                    On Jun 30, 1:49 pm, ron.h.h...@gmai l.com wrote:
                    >
                    var m;
                    if ( m = myString.match( /^.*\(\$(\d+\.\d {2})\)$/ ) ) {
                    alert( "Matched: " + m[1] );
                    >
                    }
                    >
                    may serve better.
                    >
                    And since it's now used in a match rather than replace, the regular
                    expression can be further simplified to:

                    /\(\$(\d+\.\d{2} )\)$/

                    --
                    ../rh


                    Comment

                    Working...