在 camelCased JavaScript 字符串的正则表达式中插入退格键

Insert backspace in regex in camelCased JavaScript string

我在JavaScript中有一个字符串,在某些地方对应于一个正则表达式(小写字母后跟大写字母),我想在大写字母和小写字母之间插入退格符。

这是一个例子:

Manufacturer: SamsungWarranty: 12 monthsUnlocking: Any operatorIris scanner: NoScreen size: 5.7 inchesDisplay resolution: 2560 x 1440 pixels

应该变成:

Manufacturer: Samsung

Warranty: 12 months

Unlocking: Any operator

Iris scanner: No

Screen size: 5.7 inches

Display resolution: 2560 x 1440 pixels

您可以匹配 /([a-z])([A-Z])/g 并替换为 "\n\n",然后将 "Manufacturer: " 添加到字符串的开头。

const s = "SamsungWarranty: 12 monthsUnlocking: Any operatorIris scanner: NoScreen size: 5.7 inchesDisplay resolution: 2560 x 1440 pixels";
const res = "Manufacturer: " + s.replace(/([a-z])([A-Z])/g, "\n\n");
console.log(res);