By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Searching through String

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Steve G wrote:
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

Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
Steve G wrote:
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
Jul 23 '05 #4

P: n/a
So, if my string looks like:

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

Jul 23 '05 #5

P: n/a
Steve G wrote:
So, if my string looks like:

"Crm-its-datateam Boise" <cr********************@hp.com>
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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.