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|$)"
Sign up to request clarification or add additional context in comments.

5 Comments

when i try ur snipped it prints out only 0:0:0, 0:0:1 but where is the 0:0:2?
@marius Ah! A minor mistake. Edited the regex. Will work now.
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 You need to get the group 1 out of it. Javascript doesn't support look-behind. So, you have no other option.
I don't really understand what you mean with this, could you show any code-snippet? kind regards

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.