使用通配符匹配 URL [英] Matching URL with wildcards

查看:53
本文介绍了使用通配符匹配 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有通配符的 URL 与实际 URL 匹配.例如:

I'm trying to match URLs with wildcards in them to actual URLs. For example:

http://*google.com/*

需要匹配

http://maps.google.com

http://www.google.com/maps

解决这个问题的最佳方式是什么?

What would be the best way of going about this?

我尝试过使用正则表达式,当我手动对其进行编程时效果很好,但我不确定是否可以动态生成正则表达式,或者这是否是这种情况下的最佳做法.

I've tried using a regular expression and that works fine when I manually program it but I'm not sure whether it's possible to dynamically generate regular expressions or if that would be the best practice in this situation.

/(http|https):\/\/.*\.?google\.com\/?.*/i

非常感谢.

推荐答案

[^ ]* 替换模式中所有出现的 * - 它匹配一系列零个或多个非空格字符.

Replace all occurrences of * in the pattern with [^ ]* - it matches a sequence of zero or more non-space characters.

这样 http://*google.com/* 将变成 http://[^ ]*google.com/[^ ]*

这是完成任务的正则表达式:

Here is a regular expression to do the task:

regex = urlPattern.replace(/\*/g, "[^ ]*");

这篇关于使用通配符匹配 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆