本文介绍了用Javascript分割Pascal案例(某些案例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试使用JavaScript regex命令将 EYDLessThan5Days 转换为 EYD少于5天。有什么想法?
I've been trying to get a JavaScript regex command to turn something like EYDLessThan5Days into EYD Less Than 5 Days. Any ideas?
我使用的代码:
"EYDLessThan5Days"
.replace(/([A-Z])/g, ' $1')
.replace(/^./, function(str){ return str.toUpperCase(); });
Out: EYD少于5天
但是仍然给我错误的结果。
Out: E Y D Less Than5 Days
But still give me wrong result.
请帮帮我。谢谢。
推荐答案
尝试使用以下函数,它可以处理你可以抛出的各种字符串。如果您发现任何缺陷,请在评论中指出。
Try the following function, it's made to work with all kinds of strings you can throw at it. If you find any defects please point it out in the comments.
function camelPad(str){ return str
// Look for long acronyms and filter out the last letter
.replace(/([A-Z]+)([A-Z][a-z])/g, ' $1 $2')
// Look for lower-case letters followed by upper-case letters
.replace(/([a-z\d])([A-Z])/g, '$1 $2')
// Look for lower-case letters followed by numbers
.replace(/([a-zA-Z])(\d)/g, '$1 $2')
.replace(/^./, function(str){ return str.toUpperCase(); })
// Remove any white space left around the word
.trim();
}
// Test cases
document.body.appendChild(document.createTextNode(camelPad("EYDLessThan5Days")));
document.body.appendChild(document.createElement('br'));
document.body.appendChild(document.createTextNode(camelPad("LOLAllDayFrom10To9")));
document.body.appendChild(document.createElement('br'));
document.body.appendChild(document.createTextNode(camelPad("ILikeToStayUpTil9O'clock")));
document.body.appendChild(document.createElement('br'));
document.body.appendChild(document.createTextNode(camelPad("WhatRYouDoing?")));
document.body.appendChild(document.createElement('br'));
document.body.appendChild(document.createTextNode(camelPad("ABC")));
document.body.appendChild(document.createElement('br'));
document.body.appendChild(document.createTextNode(camelPad("ABCDEF")));
这篇关于用Javascript分割Pascal案例(某些案例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!