Pavel Vanchugov Pavel Vanchugov - 14 days ago 5
Javascript Question

Select value from array keys and skip if there is no match

I am trying to get all numbers after first comma in that text

tl.to(abstract_m, 12, {x:"271", y:"193"});
tl.set(abstract_m, {x:"0", y:"0"});
tl.to(abstract_m, 2, {x:"456", y:"424"});


Here code in action http://codepen.io/one2gov/pen/vyJKga?editors=1111

function tweenSize() {
var selectSec = $("#fname").val().split(';');
for(var key in selectSec) {
var value = selectSec[key];
if (value.split('.set') >= 0){
alert("you don't need this"+value.split('.set'));
}

else {
var ShowSec = value.split(',')[1];
alert(ShowSec);
}


The problem is js doesnt let me split arrays keys properly. I am getting desired result after getting all strokes. it looks like

here is stroke 1
here is stroke 2
here is stroke 3
...
you don't need stroke 2!

Answer

For getting all numbers in the text, you could match only numbers.

var text = 'tl.to(abstract_m, 12,  {x:"271", y:"193"});\ntl.set(abstract_m,  {x:"0", y:"0"});\ntl.to(abstract_m, 2,  {x:"456", y:"424"});',
    numbers = text.match(/\d+/g);

console.log(numbers);

Just the numerical values between commas

var text = 'tl.to(abstract_m, 12,  {x:"271", y:"193"});\ntl.set(abstract_m,  {x:"0", y:"0"});\ntl.to(abstract_m, 2,  {x:"456", y:"424"});',
    numbers = text.match(/,\s*\d+,/g),
    nice = numbers.map(function (a) {
        return a.match(/\d+/)[0];
    });

console.log(numbers);
console.log(nice);