0

I want to split a string into an array containing all leading digits as one value and the rest of the string as the second value:

3px -> Array('3','px')
01b -> Array('01','b')
01 -> Array('01','')
b -> Array('','b')
1.5 -> Array('1','.5')
300,5 -> Array('300',',5')
a1 -> Array('','a1')

In PHP you could do something like:

$matches = array();
preg_match('/([0-9]+)([^0-9]+)/',$value,$matches);

5 Answers 5

5

I'd use .match instead of split: match and capture digits, then match and capture anything else in a different capture group:

const doSplit = str => {
  const [, ...arr] = str.match(/(\d*)([\s\S]*)/);
  return arr;
};
console.log(
  doSplit('3px'),
  doSplit('01b'),
  doSplit('01'),
  doSplit('b'),
);

  • (\d*) - matches digits
  • ([\s\S]*) - matches absolutely anything after that
Sign up to request clarification or add additional context in comments.

Comments

0

just do str.match(/([0-9]+)([^0-9]+)/).splice(1) where str is the variable containing your value.

1 Comment

Seems like this does not work with 1.5 -> Array('1','.5')
0

You can do somethinglike this:

var str = "Hello world!";
var res = [str.substring(0, 1), str.substring(1)];

This stores the string in a variable, and then creates an array containing 2 elements. The first will be a substring of the string containing the value from the start to the 1st position, and the second value will be a substring containing the value from the second position, and as no second parameter is supplied, until the end.

Comments

0

Here is my take on this one. Hope it helps.
Good luck!

function parseString(str) {
  var num = [];
  var nonNum = [];
  for (let i of str.split("")) {
    if (/\D/.test(i)) {
      nonNum.push(i)
      break;
    } else {
      num.push(i)
    }
  }

  if (num.length !== 0) {
    return [num.join(""), str.substring(num.length)]
  } else {
    return (num.length === 0) ? ["", str] : [num.join(""), ""]
  }
}

console.log(parseString("3px"), parseString("01b"), parseString("01"), parseString("b"), parseString("1.5"), parseString("300,5"), parseString("a1"))


Comments

0

I feel this question points out a benefit in PHP which we do not enjoy in Javascript. I wrote a little hack for this. I hope it helps.

function split_non_numeric(str){
    let strValue = '';
    let intValue = parseInt(str, 10);
    let matchPattern = str.match(new RegExp(intValue));

    if(matchPattern === null){
        intValue = '';
        strValue = str;
    }else{
        if(matchPattern.index > 0){
            intValue = str.substr(0, matchPattern.index + matchPattern[0].length);
            strValue = str.substr(matchPattern.index + matchPattern[0].length);
        }else{
            intValue = str.slice(matchPattern.index, matchPattern[0].length)
            strValue = str.substr(matchPattern[0].length);
        }
    }

    return [intValue, strValue];
}

console.log(split_non_numeric('3px')); // [ '3', 'px' ]
console.log(split_non_numeric('01b')); // [ '01', 'b' ]
console.log(split_non_numeric('01')); // [ '01', '' ]
console.log(split_non_numeric('b')); // [ '', 'b' ]
console.log(split_non_numeric('1.5')); // [ '1', '.5' ]
console.log(split_non_numeric('300,5')); // [ '300', ',5' ]
console.log(split_non_numeric('a1')); // [ '', 'a1' ]

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.