Connecting Tech Pros Worldwide Forums | Help | Site Map

Searching through String

Steve G
Guest
 
Posts: n/a
#1: Jul 23 '05
Steve G Feb 1, 1:12 pm show options

From: "Steve G" <sgr...@computicle.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
Guest
 
Posts: n/a
#2: Jul 23 '05

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

Steve G
Guest
 
Posts: n/a
#3: Jul 23 '05

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

Dietmar Meier
Guest
 
Posts: n/a
#4: Jul 23 '05

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
Steve G
Guest
 
Posts: n/a
#5: Jul 23 '05

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 < > ???

Dietmar Meier
Guest
 
Posts: n/a
#6: Jul 23 '05

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(myNiceVar.match(/"[^"]+"\s+<([^>]+)>/))[1];

ciao, dhgm
Closed Thread