如何写这个?????????????????????(标题要长)

wtcsy 2010-01-26 09:27:09
要求就是
希望能够匹配那个name的所有属性
我写的如下 (问题多多)
ps :尽量合理的匹配
如fangfang fang可以匹配 但是 fa去匹配就会报告错误
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

</head>

<body>

<input id='ss' type="text" /><input value='test' type="button" onclick="ss(document.getElementById('ss').value)" />
<script>
function ss(v){
var name ={
a : ['a','ai','an','ang','ao'],
b : ['ba','bai','ban','bang','bao','bei','ben','beng','bi','bian','biao','bie','bin','bing','bo','bu'],
c : ['ca','cai','can','cang','cao','ce','cen','ceng','cha','chai','chan','chang','chao','che','chen','cheng','chi','chong','chou','chu','chua','chuai','chuan','chuang','chui','chun','chuo','ci','cong','cou','cu','cuan','cui','cun','cuo'],
d : ['da','dai','dan','dang','dao','de','dei','den','deng','di','dia','dian','diao','die','ding','diu','dong','dou','du','duan','dui','dun','duo'],
e : ['e','en','eng','er'],
f : ['fa','fan','fang','fei','fen','feng','fiao','fo','fou','fu'],
g : ['ga','gai','gan','gang','gao','ge','gei','gen','geng','gong','gou','gu','gua','guai','guan','guang','gui','gun','guo'],
h : ['ha','hai','han','hang','hao','he','hei','hen','heng','hong','hou','hu','hua','huai','huan','huang','hui','hun','huo'],
j : ['ji','jia','jian','jiang','jiao','jie','jin','jing','jiong','jiu','ju','jun','juan','jue'],
k : ['ka','kai','kan','kang','kao','ke','ken','keng','kong','kou','ku','kua','kuai','kuan','kuang','kui','kun','kuo'],
l : ['la','lai','lan','lang','lao','le','lei','leng','li','lia','lian','liang','liao','lie','lin','ling','liu','lo','long','lou','lu','luan','lun','luo','lv','lve'],
m : ['ma','mai','man','mang','mao','me','mei','men','meng','mi','mian','miao','mie','min','ming','miu','mo','mou','mu'],
n : ['na','nai','nan','nang','nao','ne','nei','nen','neng','ni','nian','niang','niao','nie','nin','ning','niu','nong','nou','nu','nuan','nun','nuo','nv','nve'],
o : ['o','ou'],
p : ['pa','pai','pan','pang','pao','pei','pen','peng','pi','pian','piao','pie','pin','ping','po','pou','pu'],
q : ['qi','qia','qian','qiang','qiao','qie','qin','qing','qiong','qiu','qu','quan','que','qun'],
r : ['ran','rang','rao','re','ren','reng','ri','rong','rou','ru','rua','ruan','rui','run','ruo'],
s : ['sa','sai','san','sang','sao','se','sen','seng','sha','shai','shan','shang','shao','she','shei','shen','sheng','shi'
,'shou','shu','shua','shuai','shuan','shuang','shui','shun','shuo','si','song','sou','su','suan','sui','sun','suo'],
t : ['ta','tai','tan','tang','tao','te','tei','teng','ti','tian','tiao','tie','ting','tong','tou','tu','tuan','tui','tun','tuo'],
w : ['wa','wai','wan','wang','wei','wen','weng','wo','wu'],
x : ['xi','xia','xian','xiang','xiao','xie','xin','xing','xiong','xiu','xu','xuan','xue','xun'],
y : ['ya','yan','yang','yao','ye','yi','yin','ying','yo','yong','you','yu','yuan','yue','yun'],
z : ['za','zai','zan','zang','zao','ze','zei','zen','zeng','zha','zhai','zhan','zhang','zhao','zhe','zhei','zhen','zheng','zhi','zhong','zhou','zhu','zhua','zhuai','zhuan','zhuang','zhui','zhun','zhuo','zi','zong','zou','zu','zuan','zui','zun','zuo']
};
alert(testname(name,v));
};
function testname(o,v){
if(v=="")return false;
var arr ="";
var exit = true;
while(exit&&v.length>0){
arr = "";
var f = v.slice(0,1);
if(!o[f]){exit = false; return false};
for(var i=0,l=o[f].length;i<l;i++)
{
if((new RegExp("^("+o[f][i]+")")).test(v))
arr = arr.length>o[f][i].length?arr:o[f][i];
}
if(arr.length==0)
{
exit = false;
}else{
v = v.slice(arr.length,v.length);
}
}
return arr.length>0?true:false;
}

</script>
</script>
</body>
</html>
...全文
198 9 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wtcsy 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zswang 的回复:]
看来简单递归就好了。
JScript codefunction testname(o, v) {if (!o||!v)returnfalse;var dict= o[v.charAt(0)];if (!dict)returnfalse;for (var i=0; i< dict.length; i++) {if (dict[i]== v)returntrue;if (v.indexOf(dict[i])==0&&
testname(o, v.substr(dict[i].length)))returntrue;
}returnfalse;
}
[/Quote]
看明白了 果然厉害.............
道光2008 2010-01-26
  • 打赏
  • 举报
回复
直接把name中的a,b等值定义为字符串,用字符串查找的模式得啦
王集鹄 2010-01-26
  • 打赏
  • 举报
回复
看来简单递归就好了。
function testname(o, v) {
if (!o || !v) return false;
var dict = o[v.charAt(0)];
if (!dict) return false;
for (var i = 0; i < dict.length; i++) {
if (dict[i] == v) return true;
if (v.indexOf(dict[i]) == 0 &&
testname(o, v.substr(dict[i].length))) return true;
}
return false;
}
myblind 2010-01-26
  • 打赏
  • 举报
回复
up
yixianggao 2010-01-26
  • 打赏
  • 举报
回复
晕,既然代码没问题贴那么多干嘛,lz 还是把匹配算法想清楚吧,

至少先写明白算法的基本判断逻辑以后再编码!
王集鹄 2010-01-26
  • 打赏
  • 举报
回复

这个问题是一个算法问题:判断字符串是否全部由拼音组成。
扫描按最长匹配扫描必然会出现这个问题,应该按回溯
phoeni_xin 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sohighthesky 的回复:]
去掉一个结束的script标签后没错误
[/Quote]
wtcsy 2010-01-26
  • 打赏
  • 举报
回复
代码没什么问题
只是不是很匹配
如 wangun 分wan gun是符合里面的
但是按我写的就会 分为wang un 这个会返回false
sohighthesky 2010-01-26
  • 打赏
  • 举报
回复
去掉一个结束的script标签后没错误
发帖
JavaScript

8.7w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2010-01-26 09:27
社区公告
暂无公告