1

I have collection of string like '36*' , '95' , '103*', '1001*' , '2301'.

I want to separate these into two parts the numerical part into integer and the asterisks part. eg: take 36 from first, 95 from second, 103 from third and so on. I add them all. If asterisk is found then multiply the number with some factor.

The asterisk is always at the end.

4
  • regular expression Commented Apr 12, 2016 at 14:17
  • explain more what do you really want to do? Commented Apr 12, 2016 at 14:19
  • I want to separate these into two parts the numerical part into integer and the asterisks part. eg: take 36 from first, 95 from second, 103 from third and so on. I add them all. If asterisk is found then multiply the number with some factor. Commented Apr 12, 2016 at 14:23
  • You mean something like this '36' * factor , '95' , '103' * factor, '1001' * factor , '2301'? Commented Apr 12, 2016 at 14:39

4 Answers 4

1

You can extract out the parts you need using a regex.

var values = ['36*', '95', '103*', '1001*', '2301'];

var parts = values.map(function(x) {
  var match = /(\d+)(\/|\*|\+|\-)?/.exec(x);

  return {
    numericPart: match[1],
    operator: match[2]
  };
});

You can then access the numeric or operator part for each element in your original array. i.e. parts[2].numericPart == 95

The regex (\d+)(\/|\*|\+|\-)? can be broken down as follows;

(\d+) - matches 1 or more digits and stores them in capture group 1
(\/|\*|\+|\-)? - matches a /*+- and stores it in capture group 2, this matches 0 or 1 times
Sign up to request clarification or add additional context in comments.

Comments

0

This function will take an input and will return the number & special symbol(*).

function _seperateNum(value){
    var returnObj={}; // This object will contain number * special character(if)
     var replaceChar=""; 
     if(value.indexOf("*") !=-1){ // check if input has *
        replaceChar = value.replace(/[*]/g, ''); // replace it
       returnObj.num = parseInt(replaceChar); // convert to number
        returnObj.char = "*";
      }
      else{ // if no special charcater
       returnObj.num = parseInt(value);
     }
     return returnObj;
    }
    var a =_seperateNum("36*");
    console.log(a.num);

Jsfiddle

Comments

0
    var 
        arr = ['36*' , '95' , '103*', '1001*' , '2301'],
        sum = 0;
    for (var i in arr) {
        var item = arr[i];
        sum += parseInt(item.indexOf('*') != -1 ? item.replace('*', '') : item);
    }
    alert(sum);

Comments

0
var arr = ['36*' , '95' , '103*', '1001*' , '2301'];
var t = [];
var f = [];
arr.forEach(function(e){if(e.indexOf("*") > 0){ t.push(e) } else { f.push(e) } } );

console.log(t); //asterisk values
#=> ["36*", "103*", "1001*"]
console.log(f); // other value
#=> ["95", "2301"]

if you want to multiple the each element of the array of asterisk elements to a factor like 10

arr.forEach(function(e){if(e.indexOf("*") > 0){ t.push(parseInt(e)* 10) } else { f.push(parseInt(e)) } } );

 console.log(t); //asterisk values
#=> [360, 1030, 10010]
console.log(f); // other value
#=> [95, 2301]

Comments

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.