gpt4 book ai didi

javascript - 将 greasemonkey 通配符转换为正则表达式

转载 作者:行者123 更新时间:2023-11-30 09:05:41 24 4
gpt4 key购买 nike

是否有一个简单的替换函数可以将 greasemonkey 通配符更改为正则表达式模式?

我想将 'http://www.google.com/*/index.html' 转换成 /^http://www.google.com\/\w\/index.html$/

我希望能够在 javascript 中使用 greasmonkey wildchar 测试 url

最佳答案

当然,我可能还没有用尽通配符的所有选项(我也没有用于试验的庞大测试数据库),但这是我想出的:

String.prototype.toRegExp = function()
{
var result = this.replace(/([\/\(\)\[\]\.\?])/g,'\\$1');
result = result.replace('*','.*');
return new RegExp(result);
}

用法:

var re = "http://www.google.com/*".toRegExp();

关于javascript - 将 greasemonkey 通配符转换为正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5236821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com