HighDots Forums  

RegEx help

Javascript JavaScript language (comp.lang.javascript)


Discuss RegEx help in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
neptune6jun44@gmail.com
 
Posts: n/a

Default RegEx help - 12-12-2007 , 01:32 PM






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.

Reply With Quote
  #2  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: RegEx help - 12-12-2007 , 04:08 PM






In comp.lang.javascript message <53a45a23-d00d-4c7e-a798-f1befa15358e@l3
2g2000hse.googlegroups.com>, Wed, 12 Dec 2007 10:32:14,
"neptune6jun44 (AT) gmail (DOT) com" <neptune6jun44 (AT) gmail (DOT) com> posted:

Quote:
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/sicl.../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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #3  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: RegEx help - 12-12-2007 , 06:18 PM



neptune6jun44 (AT) gmail (DOT) com wrote:
Quote:
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


Reply With Quote
  #4  
Old   
Randy Webb
 
Posts: n/a

Default Re: RegEx help - 12-12-2007 , 09:24 PM



Thomas 'PointedEars' Lahn said the following on 12/12/2007 6:18 PM:
Quote:
neptune6jun44 (AT) gmail (DOT) com wrote:
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.
Writing Regular Expressions for Javascript/JScript is *very* on-topic in
this group. And to say otherwise is ignorant.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #5  
Old   
RobG
 
Posts: n/a

Default Re: RegEx help - 12-12-2007 , 09:32 PM



On Dec 13, 4:32 am, "neptune6ju... (AT) gmail (DOT) com"
<neptune6ju... (AT) gmail (DOT) com> wrote:
Quote:
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'];


--
Rob


Reply With Quote
  #6  
Old   
Steve Swift
 
Posts: n/a

Default Re: RegEx help - 12-13-2007 , 01:21 AM



Quote:
I have a string like A:BCD:EFG and I need to populate a variable with
'BCD'.
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.


--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk


Reply With Quote
  #7  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: RegEx help - 12-13-2007 , 11:24 AM



On 13 Dez., 03:24, Randy Webb <HikksNotAtH... (AT) aol (DOT) com> wrote:
Quote:
Thomas 'PointedEars' Lahn said the following on 12/12/2007 6:18 PM:
How to write Regular Expressions in general is off-topic here as those can
^^^^^^^^^^
be employed in many programming languages.

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

Reply With Quote
  #8  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: RegEx help - 12-13-2007 , 11:25 AM



On 13 Dez., 07:21, Steve Swift <Steve.J.Sw... (AT) gmail (DOT) com> wrote:
Quote:
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 `::'.

Quote:
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


Reply With Quote
  #9  
Old   
Randy Webb
 
Posts: n/a

Default Re: RegEx help - 12-13-2007 , 11:35 AM



Thomas 'PointedEars' Lahn said the following on 12/13/2007 11:24 AM:
Quote:
On 13 Dez., 03:24, Randy Webb <HikksNotAtH... (AT) aol (DOT) com> wrote:
Thomas 'PointedEars' Lahn said the following on 12/12/2007 6:18 PM:
How to write Regular Expressions in general is off-topic here as those can
^^^^^^^^^^
be employed in many programming languages.
Writing Regular Expressions for Javascript/JScript is *very* on-topic in
this group. And to say otherwise is ignorant
The OP posted in a javascript group.
The OP asked for a Regular Expression.
Only Thomas could surmise he wanted anything but a JS Regular Expression
and start talking about RegEx "in general".

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #10  
Old   
Steve Swift
 
Posts: n/a

Default Re: RegEx help - 12-14-2007 , 02:04 AM



Thomas 'PointedEars' Lahn wrote:
Quote:
This will match also `::'.
If that's the data then "" is the answer.

Quote:
Is that a book?
No: http://weitz.de/regex-coach/ - but there are many of these. I found
this one usable and informative. I know just enough regex to get it
wrong nearly every time until I've tested/fixed my first guess with
Regex Coach.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.