是否有一个简单的替换函数可以将 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/