日期:2014-05-16 浏览次数:20414 次
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script language="JavaScript"> <!-- function word2number(w) { var e = "零一二三四五六七八九"; var ew = ["十","百","千"]; var ej = ["万","亿"]; var rss = "^(["+e+ew.join("")+"]+"+ej[1]+")?(["+e+ew.join("")+"]+"+ej[0]+")?(["+e+ew.join("")+"]+)?$"; // ^([零一二三四五六七八九十百千]+亿)?([零一二三四五六七八九十百千]+万)?([零一二三四五六七八九十百千]+)?$ var arr = new RegExp(rss).exec(w); function foh(str){ str = new String(str); var a=0; if(str.indexOf(ew[0])==0)a=10; str=str.replace(new RegExp(e.charAt(0),"g"),""); if(new RegExp("(["+e+"])$").test(str)) a+=e.indexOf(RegExp.$1); if(new RegExp("(["+e+"])"+ew[0]).test(str)) a+=e.indexOf(RegExp.$1)*10; if(new RegExp("(["+e+"])"+ew[1]).test(str)) a+=e.indexOf(RegExp.$1)*100; if(new RegExp("(["+e+"])"+ew[2]).test(str)) a+=e.indexOf(RegExp.$1)*1000; return a; } return foh(arr[1])*100000000+foh(arr[2])*10000+foh(arr[3]); } alert(word2number("二十五")); alert(word2number("五百零二")); alert(word2number("四千五百")); alert(word2number("七十亿零八百五十万四千五百零二")); //--> </script> </head> <body> </body> </html>