从字符串中拆分字母和数字部分的最佳方法是什么

What's the best way to split the letter and number sections from a string?

本文关键字:最佳 方法 是什么 数字部 字符串 拆分      更新时间:2023-09-26

假设我有一个字符串"AB12",抓取"AB""12"两部分的最佳(最不麻烦)方法是什么。

其他要拆分的有效字符串可以是 "A1""AA1""A12" 。这是针对JavaScript电子表格应用程序,因此字符串始终与电子表格范围相关。

var parts= "AA1".match(/([A-Za-z]+)([0-9]+)/);
#parts[1] = AA
#parts[2] = 1
好吧,

您可以使用匹配方法

取信:

console.log("hello 345".match(/[a-zA-Z]/g).join(''))

取字符串:

console.log("hello 345".match(/[0-9]/g).join(''))

http://jsbin.com/IvIFEWo/1/edit