const regex = /[A-Za-z]([a-z]+)?/g;
const regexWithNumbers = /[A-Za-z0-9]([a-z0-9]+)?/g;
console.log('456-456_43265'.match(regex)) // null
console.log('456-456_43265'.match(regexWithNumbers)) // ["456", "456", "43265"]
console.log('ThisIs-an_example'.match(regex)) // ["This", "Is", "an", "example"]
console.log('ThisIs-an_example'.match(regexWithNumbers)) // ["This", "Is", "an", "example"]
console.log('This0Is-an_example'.match(regex)) // ["This", "Is", "an", "example"]
console.log('This0Is-an_example'.match(regexWithNumbers)) // ["This0", "Is", "an", "example"]
console.log('ThisIs-an_0example'.match(regex)) // ["This", "Is", "an", "example"]
console.log('ThisIs-an_0example'.match(regexWithNumbers)) // ["This", "Is", "an", "0example"]
console.log('ThisIs-a0n_example'.match(regex)) // ["This", "Is", "a", "n", "example"]
console.log('ThisIs-a0n_example'.match(regexWithNumbers)) // ["This", "Is", "a0n", "example"]
console.log('ThisIs-an_exa0mple'.match(regex)) // ["This", "Is", "an", "exa", "mple"]
console.log('ThisIs-an_exa0mple'.match(regexWithNumbers)) // ["This", "Is", "an", "exa0mple"]
这个正则表达式似乎有效,但是您不能使用 split 方法,因为它会删除您从原始字符串中传递的字符,如果您不希望将诸如 ThisIs 之类的内容分开,这是可能的。
所以,改为使用String.proptotype.match() 方法,它将返回一个包含匹配项的数组