0

How can i get values from value pos:

var pos = (59.9477, 59.9477)
var arr = pos.replace(/[^\d|,]/g, "").split(",");
console.log(arr);

2
  • is pos a string??? And what is your desired output?? Commented Feb 17, 2017 at 21:29
  • 1
    Do you mean var pos = '(59.9477, 59.9477)' ? Also what is your expected output? Commented Feb 17, 2017 at 21:30

3 Answers 3

2
  1. pos should be a string.
  2. You have to exclude . too from being removed.
  3. The | (or) inside the set has no use.

var pos = "(59.9477, 59.9477)";
var arr = pos.replace(/[^\d,.]/g, "").split(",");
console.log(arr);

Another way to do it:

You can use match to get the result using this regular expression /\d*\.?\d+/g. Like this:

var pos = "(59.9477, 59.9477)";
var arr = pos.match(/\d*\.?\d+/g);
console.log(arr);

Sign up to request clarification or add additional context in comments.

4 Comments

Just a small type in the first snippet: you need to escape the period there too: \.
@MacPrawn The only thing that needs escaping inside the set is ].
"." is a reserved character, so you're actually going to replace everything that is not a digit, a comma, or "one character" - no?
@MacPrawn Inside a set, . will be the litteral . not the any-character ., / will be / not the end of regex, .... the only thing that cause problems is ] and -!
1

How can i get values from value pos:

Based on your code, and your question with var pos = (59.9477, 59.9477), you should know that console.log(pos) // => 59.9477 and typeof post // => "number" and replace method and regex is for String type only. So I guess your variable could be var pos = "(59.9477, 59.9477)" which is String.

var arr = pos.replace(/[^\d|,]/g, "").split(","); your regex /[^\d|,]/g, this mean match all except number and comma, but I think you would want to keep the decimal right? Other will you will get back ["599477", "599477"]. I guess you want ["59.9477", "59.9477"], if so your code should be var arr = pos.replace(/[^\d.,]/g, "").split(",");

Note: you do not need | for or when using NOT [^]

Comments

0

Just use quotes, instead of ():

var pos = "59.9477, 59.9477";
var arr = pos.replace(/[^\d|,]/g, "").split(",");
console.log(arr);

2 Comments

my problom is that iam getting this value with quotes in REST json , and i need to get theese lat and long values out of quotes?
So what's your data ? a string, array or json object ? there is no such thing as values between () in javascript. () is used to define or execute functions only..

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.