我想要一个使用javascript转换的通用方法
第一层,二楼,底层的值将转换为以下格式,
一楼:Gr。楼
一楼:1楼 二十楼:20楼
现在我正在使用以下代码 但这不是正确的方式,因为我不能写所有楼层(50楼,90楼)所以请帮助我这个
function validpress(iKeyascii)
{
if( iKeyascii=='77'||iKeyascii=='76'||iKeyascii=='68'||iKeyascii=='73'||iKeyascii=='88'||iKeyascii=='86'||iKeyascii=='67')
{
alert("Roman numbers are not allowed.");
return false;
}
var txt= document.getElementById('<%=TextBox1.ClientID %>').value;
//document.getElementById('<%=TextBox1.ClientID %>').value = txt.toUpperCase();
var patternforGFloor = new RegExp('\\b[G|g][R|r][O|o][U|u][n|N][D|d]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor1Floor = new RegExp('\\b[F|f][I|i][R|r][S|s][T|t]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor2Floor = new RegExp('\\b[S|s][E|e][C|c][O|o][N|n][D|d]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor3Floor = new RegExp('\\b[T|t][H|h][I|i][R|r][D|d]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor4Floor = new RegExp('\\b[F|f][O|o][U|u][R|r][T|t][H|h]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor5Floor = new RegExp('\\b[F|f][I|i][F|f][T|t][H|h]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
if (txt.match(patternforGFloor) )
{
document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternforGFloor,"Gr.Floor");
}
if (txt.match(patternfor1Floor))
{
document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternfor1Floor,"1st. Floor");
}
if (txt.match(patternfor2Floor) )
{
document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternfor2Floor,"2nd.Floor");
}
if (txt.match(patternfor3Floor))
{
document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternfor3Floor,"3rd. Floor");
}
if (txt.match( patternfor4Floor))
{
document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace( patternfor4Floor,"4th. Floor");
}
if (txt.match( patternfor5Floor))
{
document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace( patternfor5Floor,"5th. Floor");
}
}
答案 0 :(得分:3)
首先,你的Regexen很疯狂。使用不区分大小写标记/i
并使用正则表达式文字,这样您就不必两次逃避所有内容:
var patternfor1Floor = /\bfirst\s*floor\b/i;
其次,你需要找到英语中的模式并重复使用它们:
first, twenty-first, one-hundred-twenty-first
second, twenty-second, one-hundred-twenty-second
“第一”和“第二”部分是重复的,因此您只需要定义first
到ninth
及其值一次。同样适用于twenty
,thirty
和hundred
,thousand
等。因此,您可以将one-hundred-twenty-first
之类的数字分解为“(一百)”, “二十”和“第一”并添加值。 “十一”到“十九”是您需要单独识别的例外情况。
通过查看最后一位数字,在号码末尾添加st
,nd
,rd
或th
是微不足道的。
我不能也不会在这里给你一个完整的解决方案,试着找到这个。
答案 1 :(得分:1)
您需要确定一串字的数字, 并使用正确的序数后缀返回数字。
如果你的用户是好的拼写者(或者他们使用的是数字而不是单词),这将最多可以达到999个楼层。
不要对按键执行长时间的计算 - 当用户完成输入,更改或onblur时执行这些操作。
Run=window.Run || {};
Number.prototype.nth= function(){
if(this> 3 && this<21) return this+'th';
var suffix= this%10;
switch(suffix){
case 1:return this+'st';
case 2:return this+'nd';
case 3:return this+'rd';
default:return this+'th';
}
}
Run.makeFloors= function(){
var nums= [
['ground', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred']
],
floor={
first:1, second:2, third:3, fifth:5, eighth:8, ninth:9, twelfth:12
};
nums[0].forEach(function(itm, i){
floor[itm]= i;
if(itm>3)floor[itm+'th']= i;
});
nums[1].forEach(function(itm, i){
if(itm) floor[itm]= 10*i;
floor[itm+'th']= i;
if(itm<100) floor[itm.slice(0, -2)+'tieth']= i;
});
return floor;
}
Run.getFloor= function(s){
Run.floor= Run.floor || Run.makeFloors();
if(parseInt(s)) return parseInt(s).nth()+' Floor';
s= s.toLowerCase().replace(/(\W+|and|floor)/g, '').replace(/y(\S)/, 'y $1');
var F= 0, nums, ax, s2;
var ax= s.indexOf('hundred');
if(ax!= -1){
s= s.split(/hundred/);
s2= s[0]? floor[s[0]]:1;
F= 100*s2;
s= s[1];
}
if(floor[s]) F+= floor[s];
else{
s= s.split(/ +/);
if(floor[s[0]]) F+= floor[s[0]];
if(floor[s[1]]) F+= floor[s[1]];
}
if(F== 0) return 'Gr. Floor';
return F.nth()+' Floor';
}
Run.getFloor('Fourth')= 4th Floor
Run.getFloor('twenty second')= 22nd Floor
Run.getFloor('eighty-fifth')= 85th Floor
Run.getFloor('ground')= Gr.Floor
Run.getFloor('Twelve')= 12th Floor
Run.getFloor(21)= 21st Floor
Run.getFloor('one hundred and seven')= 107th Floor
Run.getFloor('nine hundred and ninety nine')= 999th Floor
//to support old browsers you need to shim Array.forEach
Array.prototype.forEach= [].forEach || function(fun, scope){
var T= this, L= T.length, i= 0;
if(typeof fun== 'function'){
while(i< L){
if(i in T){
fun.call(scope, T[i], i, T);
}
++i;
}
}
return T;
}
答案 2 :(得分:-1)
您还可以使用Javascript HASHTable。
var myHash = new Hashtable();
myHash.put("First Floor", "1st");
myHash.put("Second Floor", "2nd");
alert( myHash.get('First Floor') ); // "1st"
您可以从here.
获取此JS函数This link might also be helpful.
修改强>
这种方法的缺点是你不能处理大/小的情况。 但是您可以在小写字母中添加HashTable中的所有键,并将输入键转换为小写以获得正确的值。干杯!