我有一个包含大量项目的字符串数组(这里显示了一个包含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>