我有一个包含大量项目的字符串数组(此处显示了包含 3 个元素的样本大小数组(:
['helloWorld', 'helloEarth', '28rounds']
我正在使用Lodash的StartCase将它们转换为StartCase字符串。
因此,输出数组将是:['Hello World', 'Hello Earth', '28 Rounds']
但是我希望即使我们传递到 lodash StartCase 函数中,也不要更改一些项目。是否有一个覆盖运算符,我可以将字符串包装到其中,以便 lodash 不会转换它。
我希望按原样打印28rounds
但其他内容转换为 StartCase
我不想做相等性检查,那将是紧密耦合的解决方案。
您可以使用 String.replace()
(或 lodash 的替换(和 RegExp 来捕获字母序列,并使用 _.startCase()
将它们替换为新字符串:
const arr = ['helloWorld', 'helloEarth', '28rounds']
const startCaseNoNumbers = str => str.replace(/^([A-Z]?[a-z]+)+/, _.startCase)
const result = arr.map(startCaseNoNumbers)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>