JS实现通配符
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body> </body> </html> <script language="JavaScript"> var PATTERN_LINE_START = "^"; var PATTERN_LINE_END = "$"; var META_CHARACTERS = ['$', '^', '[', ']', '(', ')', '{', '}', '', '+', '.', '\\']; function wildcard(pattern,word){ var result = PATTERN_LINE_START; for(var i=0;i<pattern.length;i++){ var ch = pattern.charAt(i); if(metaSearch(ch)){ result += "\\" + ch; continue; }else{ switch (ch) { case '*': result += ".*"; break; case '?': result += ".{0,1}"; break; default: result += ch; } } } result += PATTERN_LINE_END; if(word.match(result) == null){ return false; } return true; } function metaSearch(ch){ for(var metaCh in META_CHARACTERS){ if(ch == metaCh ){ return true; } } return false; } var data = "*.jpg"; alert(wildcard(data,"asd.jpgu")); </script>
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索function
, document
, result
, var
, 通配符
pattern
js通配符、js字符串通配符、js中通配符、js src 通配符、js正则通配符,以便于您获取更多的相关知识。
时间: 2024-09-13 21:54:30