-1

I have strings like: '1234picas' '1234 px' '145.4us'

I want to split them in two parts: the numeric part and the non numeric one. For example: '1234.4us' need to be splitted in '1234.4' and 'us'. I am tempted to insert a separator between the last digit and the non numeric and use split but is there a better way to do this in JavaScript

Thanks

Note: this is not the slitting of a string at special char. it could be converted to that but this is what I am trying to avoid.

Jose Cabrera Zuniga
  • 2,348
  • 3
  • 31
  • 56
  • Use a Regular Expression – 3vts Apr 17 '18 at 21:18
  • I was reading RegExp for Scala and I got more confused... I'll look into this again – Jose Cabrera Zuniga Apr 17 '18 at 21:20
  • Also https://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character – TylerH Apr 17 '18 at 21:31
  • And https://www.google.com/search?client=firefox-b-1-ab&ei=fWfWWrLZL-2E_QbxoLVI&q=how+to+split+a+string+with+special+characters+site%3Astackoverflow.com+javascript&oq=how+to+split+a+string+with+special+characters+site%3Astackoverflow.com+javascript&gs_l=psy-ab.3...4679.5783.0.5845.11.9.0.0.0.0.131.688.4j4.8.0....0...1c.1.64.psy-ab..3.0.0....0.L2FlEis3IH0 for nearly 1 million other results – TylerH Apr 17 '18 at 21:31
  • This is not the splitting of a string at special character. This could be a path to follow but I am trying to avoid it. – Jose Cabrera Zuniga Apr 17 '18 at 22:46

6 Answers6

1

here's one way to do it using parseFloat() and slice() , you can add it to the Sting.prototype if you want :

const str1 = '1234picas',
  str2 = '1234 px',
  str3 = '145.4us';

String.prototype.split = function () {
  const num = parseFloat(this);
  const alph = this.slice(num.toString().length, this.length)

  return {
    num,
    alph
  }
}


console.log(str1.split());
console.log(str2.split());
console.log(str3.split());
Taki
  • 17,320
  • 4
  • 26
  • 47
1

You can do it using String.prototype.match():

const a = '1234picas';
const b = '1234 px';
const c = '145.4us';

function split(input) {
  const splitArray = input.match(/([\d\.]+)(.*)/); // match only digits and decimal points in the first group and match the rest of the string in second group
  
  return {
    numeric: splitArray[1],
    nonnumeric: splitArray[2],
  };
}

console.log(split(a));
console.log(split(b));
console.log(split(c));

Regex explanation | Regex101

Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
1

You can use .split with a regex using a lookahead:

str.split(/(?=[^\d.-])/g))
   .map(y => [
         y[0],
         y.slice(1).join('').trim()
   ])

x = ["1234picas", "1234 px", "145.4us"];

console.log(x.map(y => 
    y.split(/(?=[^\d.-])/g))
     .map(y => [
         y[0],
         y.slice(1).join('').trim()
     ])
)
dave
  • 62,300
  • 5
  • 72
  • 93
1

This may help you:

var a = "4343.453fsdakfjdsa";
a.match(/[a-zA-Z]+|[\d\.?]+/ig);

MDN for String.match

It basically says match the alphabetical letters OR match numbers with an optional period. The ig is insensitive (which is not really needed) and global as in don't return on the first match, keep going until all of the string has been parsed.

Brenton
  • 11
  • 3
1

Regex!
Here is how it works: https://regex101.com/r/XbI7Mq/1

const test = ['1234picas', '1234 px', '145.4us', 'no'];
const regex = /^(\d+\.?\d+)\s?(.*)/;
test.forEach(i => {
  const result = regex.exec(i);
  if (result) {
    console.log(result[1], result[2])
  }
});
Jonathan
  • 8,771
  • 4
  • 41
  • 78
1

You can do like this in javascript to split:

var myString = '145.4us';
var splits = myString.split(/(\d+\.?\d+)/);
console.log(splits);