RegEx help

N

neptune6jun44

RegEx noob here, so apologies for such an easy issue. :)

I have a string like A:BCD:EFG and I need to populate a variable with
'BCD'.

What is the RegEx syntax for matching the content between the two
colons?

Thanks.
 
D

Dr J R Stockton

In comp.lang.javascript message <53a45a23-d00d-4c7e-a798-f1befa15358e@l3
2g2000hse.googlegroups.com>, Wed, 12 Dec 2007 10:32:14,
I have a string like A:BCD:EFG and I need to populate a variable with
'BCD'.

What is the RegEx syntax for matching the content between the two
colons?

S = "A:BCD:EFG"

S.replace(/.*:(.*):.*/, "$1")
or
M = S.match(/:(.*):/)[1]

The match method is more extensible.

See
<http://www.ifi.uzh.ch/groups/CL/siclemat/lehre/javascript/JS13/contents
..htm> Client-Side JavaScript Reference - 1998; legible ;
& <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
T

Thomas 'PointedEars' Lahn

RegEx noob here, so apologies for such an easy issue. :)

I have a string like A:BCD:EFG and I need to populate a variable with
'BCD'.

What is the RegEx syntax for matching the content between the two
colons?

Assuming "BCD" is variant, for example

:[^:]+:

(a colon followed by a positive number of non-colons followed by a colon.)

How to write Regular Expressions in general is off-topic here as those can
be employed in many programming languages. Try comp.lang.misc next time or
STFW (and find "Mastering Regular Expressions" by Jeffrey E. F. Friedl at
O'Reilly Online).


PointedEars
 
R

RobG

RegEx noob here, so apologies for such an easy issue. :)

I have a string like A:BCD:EFG and I need to populate a variable with
'BCD'.

What is the RegEx syntax for matching the content between the two
colons?

You can also split the elements into an array using split:

var s = 'A:BCD:EFG';
var sArray = s.split(':');

==> sArray = ['A', 'BCD', 'EFG'];
 
S

Steve Swift

I have a string like A:BCD:EFG and I need to populate a variable with

this works for me:

:(.*):

but I have little or no concept of putting things in variables. I use
"The Regex Coach" to ensure that things work as I would expect them to.
 
T

Thomas 'PointedEars' Lahn

Thomas 'PointedEars' Lahn said the following on 12/12/2007 6:18 PM:

Writing Regular Expressions for Javascript/JScript is *very* on-topic in
this group. And to say otherwise is ignorant
 
T

Thomas 'PointedEars' Lahn

this works for me:

:(.*):

This will match also `::'.
but I have little or no concept of putting things in variables. I use
"The Regex Coach" to ensure that things work as I would expect them to.

Is that a book?


PointedEars
 
T

Thomas 'PointedEars' Lahn

Steve said:
If that's the data then "" is the answer.

Not destroying the context might have been wise in order to understand what
I was aiming at.


PointedEars
 
S

Steve Swift

Thomas said:
Not destroying the context might have been wise in order to understand what
I was aiming at.


PointedEars

You're in the wrong newsgroup. You should aim such comments at the
cause: Thunderbird.
 
T

Thomas 'PointedEars' Lahn

Steve said:
Thomas said:
Steve said:
Thomas 'PointedEars' Lahn wrote:
I have a string like A:BCD:EFG and I need to populate a variable with ^^^
'BCD'.

this works for me:

:(.*):
This will match also `::'.
If that's the data then "" is the answer.
Not destroying the context might have been wise in order to understand what
I was aiming at.
[...]

You're in the wrong newsgroup. You should aim such comments at the
cause: Thunderbird.

I use exactly the same version of Thunderbird like you do and therefore I
know that is an outright lie^W winding around *your* failing. You should
be ashamed for attributing it to the software.

Please trim your quotes while retaining the context.


F'up2 poster

PointedEars
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top