A W*_* It 160 javascript regex
我一直试图获得一个JavaScript正则表达式命令来转换"thisString"
成类似"This String"
但是最接近我正在替换一个字母,导致像"Thi String"
或"This tring"
.有任何想法吗?
为了澄清我可以处理大写字母的简单性,我对RegEx并不那么强大,而且分裂"somethingLikeThis"
成"something Like This"
我遇到麻烦的地方.
Vin*_*ert 365
"thisStringIsGood"
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, function(str){ return str.toUpperCase(); })
Run Code Online (Sandbox Code Playgroud)
显示器
This String Is Good
Run Code Online (Sandbox Code Playgroud)
(function() {
var $textbox = $('#textbox'),
$result = $('#result'),
splitter = function() {
$result.html($textbox.val()
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, function(str) {
return str.toUpperCase();
}));
};
$textbox.on('input', splitter);
splitter();
}());
Run Code Online (Sandbox Code Playgroud)
#result {
margin-top: 10px;
padding-top: 10px;
border-top: solid 1px #c3c3c3;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
Text to split
<input id="textbox" value="thisStringIsGood" />
</div>
<div id="result"></div>
Run Code Online (Sandbox Code Playgroud)
Mat*_*ebe 85
我对此很感兴趣,特别是在处理大写序列时,例如在xmlHTTPRequest中.列出的函数将产生"Xml HTTP Request"或"Xml HTTPRequest",我的产生"Xml HTTP Request".
function unCamelCase (str){
return str
// insert a space between lower & upper
.replace(/([a-z])([A-Z])/g, '$1 $2')
// space before last upper in a sequence followed by lower
.replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3')
// uppercase the first character
.replace(/^./, function(str){ return str.toUpperCase(); })
}
Run Code Online (Sandbox Code Playgroud)
在gist中还有一个String.prototype版本.
Ple*_*and 22
这可以通过regex lookahead(现场演示)简洁地完成:
function splitCamelCaseToString(s) {
return s.split(/(?=[A-Z])/).join(' ');
}
Run Code Online (Sandbox Code Playgroud)
(我认为g
(全局)标志是必要的,但奇怪的是,它不是在这种特殊情况下.)
使用lookahead split
确保不会消耗匹配的大写字母,并且如果需要处理UpperCamelCase,则避免处理前导空格.要将每个字母的首字母大写,您可以使用:
function splitCamelCaseToString(s) {
return s.split(/(?=[A-Z])/).map(function(p) {
return p.charAt(0).toUpperCase() + p.slice(1);
}).join(' ');
}
Run Code Online (Sandbox Code Playgroud)
该map
阵列方法是ES5功能,但你仍然可以使用它在旧的浏览器从MDC一些代码.或者,您可以使用循环遍历数组元素for
.
小智 15
我认为这应该能够处理连续的大写字符以及简单的camelCase.
例如:someVariable => someVariable,但ABCCode!= ABC Code.
下面的正则表达式适用于您的示例,但也是在camcelCase中表示缩写的常见示例.
"somethingLikeThis"
.replace(/([a-z])([A-Z])/g, '$1 $2')
.replace(/([A-Z])([a-z])/g, ' $1$2')
.replace(/\ +/g, ' ') => "something Like This"
"someVariableWithABCCode"
.replace(/([a-z])([A-Z])/g, '$1 $2')
.replace(/([A-Z])([a-z])/g, ' $1$2')
.replace(/\ +/g, ' ') => "some Variable With ABC Code"
Run Code Online (Sandbox Code Playgroud)
您也可以按上述方式调整以大写第一个字符.
ken*_*bec 10
function spacecamel(s){
return s.replace(/([a-z])([A-Z])/g, '$1 $2');
}
Run Code Online (Sandbox Code Playgroud)
spacecamel( 'somethingLikeThis')
//返回值:类似于此
也可以处理数字的解决方案:
function capSplit(str){
return str.replace(
/(^[a-z]+)|[0-9]+|[A-Z][a-z]+|[A-Z]+(?=[A-Z][a-z]|[0-9])/g,
function(match, first){
if (first) match = match[0].toUpperCase() + match.substr(1);
return match + ' ';
}
)
}
Run Code Online (Sandbox Code Playgroud)
在这里测试[JSFiddle,没有库。没试过IE];应该是相当稳定的。