Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带星号的JavaScript模式匹配_Javascript_Html - Fatal编程技术网

带星号的JavaScript模式匹配

带星号的JavaScript模式匹配,javascript,html,Javascript,Html,我想制作一些东西来检查如下字符串: https://www.example.com/page 等于: http*/*example.com/* 或者类似的事情。 JS中有没有内置的东西可以用星号来实现这一点,或者我应该使用插件还是其他什么?没有内置的东西可以实现您所描述的功能。但是看看,这是广义的版本。您案例中的正则表达式可能是/^http.*\/\/.*example\.com.*$/ 例如: var rex=/^http.*/\/.*示例\.com.*$/; 功能测试(str){ 控制台日

我想制作一些东西来检查如下字符串:

https://www.example.com/page

等于:

http*/*example.com/*

或者类似的事情。
JS中有没有内置的东西可以用星号来实现这一点,或者我应该使用插件还是其他什么?

没有内置的东西可以实现您所描述的功能。但是看看,这是广义的版本。您案例中的正则表达式可能是
/^http.*\/\/.*example\.com.*$/

例如:

var rex=/^http.*/\/.*示例\.com.*$/;
功能测试(str){
控制台日志(str,rex.test(str));
}
测试(”https://www.example.com/page"); // 符合事实的
测试(”ttps://www.example.com/page");  // false
您可以尝试
match()
函数

  • 文件:
let str='1〕https://www.example.com/page';
让stratches=str.match(/http([\s\s]*?)\/\/([\s\s]*?)example.com([\s\s]*?)/);
让result=document.querySelector(“#result”);
如果(stratches!=null&&stratches.length>0){
result.innerHTML='Match';
}否则{
result.innerHTML='不匹配';
}

我能够创建一个函数来实现这一点,但感谢大家的回答。我将在下面发布代码

var wildcardCheck = function(i, m) {
   var regExpEscape = function(s) {
       return s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
   };
   var m = new RegExp('^' + m.split(/\*+/).map(regExpEscape).join('.*') + '$');
   return i.match(m) !== null && i.match(m).length >= 1;
};

准确地说,您要查找的是“通配符匹配”,而
*
就是通配符。您不能开箱即用,但请查看下面的答案,以获得使用regex的解决方案