我一直在尝试获取 JavaScript 正则表达式命令来将类似的内容转换为"thisString"
into "This String"
但我得到的最接近的是替换一个字母,导致类似的结果"Thi String"
or "This tring"
。有任何想法吗?
为了澄清一下,我可以处理大写字母的简单问题,我只是不太擅长正则表达式,并且拆分"somethingLikeThis"
into "something Like This"
这就是我遇到麻烦的地方。
"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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)