拆分 JS 字符串正则表达式大写后跟小写

Split JS string regex upper followed by lowercase

我有一篇很长的 javascript 可变文章,我试图拆分其中小写字符紧跟大写字符的地方

我试过使用正则表达式:

var article2 = article2.split(/(?=[A-Z][a-z])/); 

但只能在每个词上拆分

var article2 = "SplitJavaScriptString";

// you are doing this (include Small case & Upper case)
console.log(article2.split(/(?=[A-Z][a-z])/)); 

// is this what you want (exclude Small case & Upper case)
console.log(article2.split(/[A-Z][a-z]/).filter(e => e != '')); 

由于您的 JS 环境符合 ECMAScript 2018(参见 what regex features it supports), you may use lookbehinds:

.split(/(?<=[a-z])(?=[A-Z])/)

(?<=[a-z]) 模式是后视模式,需要紧挨着当前位置左侧的数字,(?=[A-Z]) 模式是正向前瞻模式,需要紧邻当前位置右侧的数字。

参见regex demo