Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a number from a string in JavaScript [duplicate]

I'd like to split strings like

'foofo21' 'bar432' 'foobar12345'

into

['foofo', '21'] ['bar', '432'] ['foobar', '12345']

Is there an easy and simple way to do this in JavaScript?

Note that the string part (for example, foofo can be in Korean instead of English).

like image 693
Bossam Avatar asked Mar 16 '17 07:03

Bossam


Video Answer


2 Answers

Second solution:

var num = "'foofo21".match(/\d+/g);
// num[0] will be 21

var letr =  "foofo21".match(/[a-zA-Z]+/g);
/* letr[0] will be foofo.
   Now both are separated, and you can make any string as you like. */
like image 104
RajeshP Avatar answered Sep 22 '22 16:09

RajeshP


You want a very basic regular expression, (\d+). This will match only digits.

whole_string="lasd行書繁1234"
split_string = whole_string.split(/(\d+)/)
console.log("Text:" + split_string[0] + " & Number:" + split_string[1])
like image 24
BioGenX Avatar answered Sep 22 '22 16:09

BioGenX