我如何转换字符串既像'helloThere'或'helloThere'到'HelloThere'在JavaScript?
当前回答
此解决方案也适用于不在[A-Z]范围内的其他Unicode字符。例如:Ä, Ö, Å。
let camelCaseToTitleCase = (s) => (
s.split("").reduce(
(acc, letter, i) => (
i === 0 || console.log(acc, letter, i)
? [...acc, letter.toUpperCase()]
: letter === letter.toUpperCase()
? [...acc, " ", letter]
: [...acc, letter]
), []
).join("")
)
const myString = "ArchipelagoOfÅland"
camelCaseToTitleCase(myString)
其他回答
const text = 'helloThereMister'; const result = text.replace(/([A-Z])/g, " $1"); const finalResult = result.charAt(0).toUpperCase() + result.slice(1); console.log (finalResult);
第一个字母大写——举个例子。注意“$1”中的空格。
当然,如果第一个字母已经大写了,你就有多余的空间可以删除。
HTTPRequest_ToServer-AndWaiting --> HTTP Request To Server And Waiting
function toSpaceCase(str) { return str .replace(/[-_]/g, ' ') /* * insert a space between lower & upper * HttpRequest => Http Request */ .replace(/([a-z])([A-Z])/g, '$1 $2') /* * space before last upper in a sequence followed by lower * XMLHttp => XML Http */ .replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3') // uppercase the first character .replace(/^./, str => str.toUpperCase()) .replace(/\s+/g, ' ') .trim(); } const input = 'HTTPRequest_ToServer-AndWaiting'; const result = toSpaceCase(input); console.log(input,'-->', result)
此解决方案也适用于不在[A-Z]范围内的其他Unicode字符。例如:Ä, Ö, Å。
let camelCaseToTitleCase = (s) => (
s.split("").reduce(
(acc, letter, i) => (
i === 0 || console.log(acc, letter, i)
? [...acc, letter.toUpperCase()]
: letter === letter.toUpperCase()
? [...acc, " ", letter]
: [...acc, letter]
), []
).join("")
)
const myString = "ArchipelagoOfÅland"
camelCaseToTitleCase(myString)
我也遇到过类似的问题,我是这样处理的:
stringValue.replace(/([A-Z]+)*([A-Z][a-z])/g, "$1 $2")
对于更健壮的解决方案:
stringValue.replace(/([A-Z]+)/g, " $1").replace(/([A-Z][a-z])/g, " $1")
http://jsfiddle.net/PeYYQ/
输入:
helloThere
HelloThere
ILoveTheUSA
iLoveTheUSA
输出:
hello There
Hello There
I Love The USA
i Love The USA
如果你处理的是大写骆驼的情况,这个片段可以帮助你,它还包含一些规格,所以你可以确保它适合你的情况。
export const fromCamelCaseToSentence = (word) =>
word
.replace(/([A-Z][a-z]+)/g, ' $1')
.replace(/([A-Z]{2,})/g, ' $1')
.replace(/\s{2,}/g, ' ')
.trim();
和规格:
describe('fromCamelCaseToSentence', () => {
test('does not fall with a single word', () => {
expect(fromCamelCaseToSentence('Approved')).toContain('Approved')
expect(fromCamelCaseToSentence('MDA')).toContain('MDA')
})
test('does not fall with an empty string', () => {
expect(fromCamelCaseToSentence('')).toContain('')
})
test('returns the separated by space words', () => {
expect(fromCamelCaseToSentence('NotApprovedStatus')).toContain('Not Approved Status')
expect(fromCamelCaseToSentence('GDBState')).toContain('GDB State')
expect(fromCamelCaseToSentence('StatusDGG')).toContain('Status DGG')
})
})
推荐文章
- 我如何使一个HTML文本框显示空时提示?
- 如何隐藏谷歌隐形reCAPTCHA徽章
- 如何测试一个字符串是否包含列表中的一个子字符串,在熊猫?
- 在JavaScript中调换2d数组
- 如何使用JavaScript停止浏览器后退按钮?
- 如何转换/解析从字符串到字符在java?
- 跟踪鼠标位置
- 如何获得<html>标签html与JavaScript / jQuery?
- 浏览器检测JavaScript?
- Javascript臭名昭著的循环问题?
- 在c#中验证字符串只包含字母
- 如何从PHP调用JavaScript函数?
- 不区分大小写的替换
- 好的Python模块模糊字符串比较?
- 不能在呈现不同组件警告时更新组件