2013-05-20 122 views
-2

我怎麼能串如何拆分字符串並保留分隔符?

str = 'a1b22c3'; 

轉換成數組,看起來像「A1,B22,C3」 jQuery中

arr = str.split("(?<=/[a-zA-Z]/)"); // DOES NOT WORK 

謝謝!

+3

','是不是在你的情況的分隔符。 – Achrome

+3

你想要分割這個字符串的具體規則是什麼?什麼邊界構成了分裂應該發生的地方?每封信都在嗎?您需要提供更多信息。 – qJake

+0

對不起!是的,我想在每封信之前拆分字符串。有一個字母有一個或兩個數字。 – user2402747

回答

5

可以使用String.match用正則表達式 - 這並不需要jQuery的

var str = 'a1b22c3'; 
// find 1 or more letters followed by 1 or more numbers 
// x will contain an array ["a1", "b22", "c3"] 
var x = str.match(/[a-zA-Z]+[0-9]+/g); 

http://jsfiddle.net/bZeP4/

相關問題