如何转换"camelCase"到“骆驼盒"? [英] How to convert "camelCase" to "Camel Case"?
本文介绍了如何转换"camelCase"到“骆驼盒"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试获取JavaScript regex命令,以将类似"thisString"
的内容转换为"This String"
,但是我得到的最接近的是替换字母,结果是类似"Thi String"
或"This tring"
的内容.有什么想法吗?
I’ve been trying to get a JavaScript regex command to turn something like "thisString"
into "This String"
but the closest I’ve gotten is replacing a letter, resulting in something like "Thi String"
or "This tring"
. Any ideas?
为了弄清楚我可以处理大写字母的简单性,我对RegEx的了解不那么强,而将"somethingLikeThis"
拆分为"something Like This"
就是我遇到的麻烦.
To clarify I can handle the simplicity of capitalizing a letter, I’m just not as strong with RegEx, and splitting "somethingLikeThis"
into "something Like This"
is where I’m having trouble.
推荐答案
"thisStringIsGood"
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, function(str){ return str.toUpperCase(); })
显示
This String Is Good
(function() {
const textbox = document.querySelector('#textbox')
const result = document.querySelector('#result')
function split() {
result.innerText = textbox.value
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, (str) => str.toUpperCase())
};
textbox.addEventListener('input', split);
split();
}());
#result {
margin-top: 1em;
padding: .5em;
background: #eee;
white-space: pre;
}
<div>
Text to split
<input id="textbox" value="thisStringIsGood" />
</div>
<div id="result"></div>
这篇关于如何转换"camelCase"到“骆驼盒"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文