简体   繁体   中英

Split a string at each number

The idea is to take a string of letters and numbers much like an algebraic expression with indices and split them up while including the index with the corresponding letter. For example: You take the expresson 2x^3 and it would be split into an array like so ["2","x^3"]

As of right now I have tried to work with the letters and the number can come later but this is what I have got so far:

expression.split(/([a-z])/);

This takes the string and splits it at every number. I'm not good at regular expression so some help on including the index of a letter with the split would be much appreciated.

Here is what you are looking for :

var string = '2x^3ui3+3-ddez2';
var regex = /(\D*\d)/g;
var array = string.match(regex);

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM