3

I want to split this line:

at myFunc http://mysite.com/test.php:13:24

into this:

at myFunc http://mysite.com/test.php
13
24

I've tried using regexes (see below) but they're wrong:

line.split( /[^http]:/ );
line.split( /[^http][:]/ );
line.split( /(?!http):/ );
line.split( /(?!http)[:]/ );

How would I do this?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • Search for "_lookbehinds_" and "_lookaheads_" in regular expressions. This should show you the way. – Tadeck Jun 16 '13 at 01:33
  • I appreciate the help but you'll notice I was using one above and it still didn't work, so I'm not understanding *lookaheads* properly (and JavaScript doesn't have lookbehind). – Don Rhummy Jun 16 '13 at 01:34
  • Javascript doesn't have the lookbehind feature – Casimir et Hippolyte Jun 16 '13 at 01:36
  • @DonRhummy: Yes, but searching for it would reveal this: http://stackoverflow.com/a/641432/548696 – Tadeck Jun 16 '13 at 01:39
  • @Tadeck How does that help me? I don't see how to apply that to my need to split on the colon except where preceeded by http. They're replacing characters to find matches, but I need all the characters, just split. Please help me understand. – Don Rhummy Jun 16 '13 at 01:40

1 Answers1

3

JavaScript doesn't have lookbehinds :(

Hack: Reverse the string

var reverse = function(s) { return s.split('').reverse().join(''); };
var parts = reverse(line).split( /:(?!ptth)/ ).map(reverse).reverse();

Tweaked problem #1: Match colon not followed by //

var parts = line.split( /:(?!\/\/)/ );

Tweaked problem #2: Match only the last two colons

var parts = line.match( /(.*):(.*):(.*)/ ) ;
parts.shift();
tom
  • 21,844
  • 6
  • 43
  • 36