Searching through String

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

    #1

    Searching through String

    Steve G Feb 1, 1:12 pm show options

    From: "Steve G" <sgr...@computi cle.com> - Find messages by this author

    Date: Tue, 01 Feb 2005 13:12:42 -0800
    Local: Tues, Feb 1 2005 1:12 pm
    Subject: Searching through String
    Reply | Reply to Author | Forward | Print | Individual Message | Show
    original | Remove | Report Abuse

    I am using Coldfusion to do some database work. I have a st­ring of
    data that is in the format of "name" <email>


    I need to have the information between the <> assigned to a ­variable,

    but the length of the field won't be the same each time.
    How can I get this done using JavaScript?


    Steve

  • Dietmar Meier

    #2
    Re: Searching through String

    Steve G wrote:
    [color=blue]
    > I have a st­ring of data that is in the format of "name" <email>
    >
    > I need to have the information between the <> assigned to a ­variable,[/color]

    Use a regular expression:

    var myString = "\"foo<bar\ " <abc@def>";
    ....
    var aSubstrs = myString.match(/"[^"]+"\s+<([^>]+)>/),
    myMatch = aSubstrs && aSubstrs[1];

    ciao, dhgm

    Comment

    • Steve G

      #3
      Re: Searching through String

      Thanks for the info! I do have a couple of questions:

      1) var myString = "\"foo<bar\ " <abc@def>";

      do I just need this in the code once?

      2) I noticed the ...... , is there more that goes between the code
      above and the var ASubstrs section?

      Thanks!

      Steve

      Comment

      • Dietmar Meier

        #4
        Re: Searching through String

        Steve G wrote:
        [color=blue]
        > 1) var myString = "\"foo<bar\ " <abc@def>";
        >
        > do I just need this in the code once?[/color]

        You don't need this at all. It just supplied a string to test with.
        You wrote "I have a string ...". I had none, so I had to get one :-)
        [color=blue]
        > 2) I noticed the ...... , is there more that goes between the code
        > above and the var ASubstrs section?[/color]

        No, I only wanted to indicate that the line above is not actually a
        part of the code that implements the substring extraction.

        ciao, dhgm

        Comment

        • Steve G

          #5
          Re: Searching through String

          So, if my string looks like:

          "Crm-its-datateam Boise" <crm-its-datateam.boise@ hp.com>
          and I wanted to capture just the info in the < > ???

          Comment

          • Dietmar Meier

            #6
            Re: Searching through String

            Steve G wrote:
            [color=blue]
            > So, if my string looks like:
            >
            > "Crm-its-datateam Boise" <crm-its-datateam.boise@ hp.com>
            > and I wanted to capture just the info in the < > ???[/color]

            Let's assume you hold your string in a variable named
            "myNiceVar" and want to assign the substring to a
            variable named "myOtherVar ", then use:

            var aSubstrs = myNiceVar.match (/"[^"]+"\s+<([^>]+)>/),
            myOtherVar = aSubstrs && aSubstrs[1];

            A shorter variant is:

            var myOtherVar = [].concat(myNiceV ar.match(/"[^"]+"\s+<([^>]+)>/))[1];

            ciao, dhgm

            Comment

            Working...