Searching through String

S

Steve G

Steve G Feb 1, 1:12 pm show options

From: "Steve G" <[email protected]> - 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
 
D

Dietmar Meier

Steve said:
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,

Use a regular expression:

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

ciao, dhgm
 
S

Steve G

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
 
D

Dietmar Meier

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

do I just need this in the code once?

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 :)
2) I noticed the ...... , is there more that goes between the code
above and the var ASubstrs section?

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
 
D

Dietmar Meier

Steve said:
So, if my string looks like:

"Crm-its-datateam Boise" <[email protected]>
and I wanted to capture just the info in the < > ???

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top