1

I have snippet javascript (web) but since i try to use RegExpression it should work almost the same.

I have a string with some coordinates in it seperated by a space charakter (last coordinate has no space after it).

var coords = "0:0 0:0:0 1:0:1 0:0:0:1 0:0:1 0:0:2";
var part = "0:0";

I want to have all the coordinates beginning with the value of part ("0:0") plus ":" and the next coordinate number. If a coordinate matches but has more than one additional "coordinate-dimensions" it shouldn't show...

For example it should show 0:0:0, 0:0:1 and 0:0:2 but NOT 0:0 (because to less dimension), 0:0:0:1 (because if the additional dimension)

What I tryed is something like:

var reg = new RegExp("(^|\\s)(0:0:\\d\\s)", "g");
alert(coords .match(reg));

But it seems not to work propperly.

Anyone has an idea?!

Kind regards!

1 Answer 1

3

You can use this regex:

"(?:^|\\s)(0:0:\\d)(?=\\s|$)"
5
  • when i try ur snipped it prints out only 0:0:0, 0:0:1 but where is the 0:0:2?
    – marius
    Commented Oct 27, 2013 at 20:30
  • @marius Ah! A minor mistake. Edited the regex. Will work now.
    – Rohit Jain
    Commented Oct 27, 2013 at 20:34
  • i've just checked there is still a mistake in it... at be beginning of every coordinate there is a whitespace in it. like " 0:0:0", " 0:0:1". Someone has an idea how to cut it out?
    – marius
    Commented Oct 28, 2013 at 13:13
  • @marius You need to get the group 1 out of it. Javascript doesn't support look-behind. So, you have no other option.
    – Rohit Jain
    Commented Oct 28, 2013 at 16:51
  • I don't really understand what you mean with this, could you show any code-snippet? kind regards
    – marius
    Commented Oct 29, 2013 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.