在Gmail到电子表格功能中使用.replace,是否有办法替换除了匹配模式的选择之外的所有内容?
在Gmail到电子表格功能中使用.replace,是否有办法替换除了匹配模式的选择之外的所有内容?
这个问题已经在这里有了答案:
目前我有一个输入框,它将检测URL并解析数据。
所以现在,我正在使用:
var urlR = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+) (?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/; var url= content.match(urlR);
问题是,当我输入像www.google.com
这样的URL时,它不起作用。而当我输入http://www.google.com
时,它就能工作。
我不太擅长正则表达式。有人可以帮我吗?
admin 更改状态以发布 2023年5月23日
(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})
将匹配以下情况
http://www.foufos.gr
https://www.foufos.gr
http://foufos.gr
http://www.foufos.gr/kino
http://werer.gr
www.foufos.gr
www.mp3.com
www.t.co
http://t.co
http://www.t.co
https://www.t.co
www.aa.com
http://aa.com
http://www.aa.com
https://www.aa.com
badurlnotvalid://www.google.com - captured url www.google.com
htpp://www.google.com - captured url www.google.com
将不匹配以下内容
www.foufos
www.foufos-.gr
www.-foufos.gr
foufos.gr
http://www.foufos
http://foufos
www.mp3#.com
var expression = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi; var regex = new RegExp(expression); var check = [ 'http://www.foufos.gr', 'https://www.foufos.gr', 'http://foufos.gr', 'http://www.foufos.gr/kino', 'http://werer.gr', 'www.foufos.gr', 'www.mp3.com', 'www.t.co', 'http://t.co', 'http://www.t.co', 'https://www.t.co', 'www.aa.com', 'http://aa.com', 'http://www.aa.com', 'https://www.aa.com', 'badurlnotvalid://www.google.com', 'htpp://www.google.com', 'www.foufos', 'www.foufos-.gr', 'www.-foufos.gr', 'foufos.gr', 'http://www.foufos', 'http://foufos', 'www.mp3#.com' ]; check.forEach(function(entry) { let match = entry.match(regex); if (match) { $("#output").append( "Success: " + entry + "Captured url: " + match + "" ); } else { $("#output").append( "Fail: " + entry + "" ); } });
如果要确保URL以HTTP/HTTPS开头的正则表达式:
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
如果不需要HTTP协议:
[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
要尝试这个,请参见http://regexr.com?37i6s,或者使用更少限制的版本http://regexr.com/3e6m0。
示例JavaScript实现:
var expression = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi; var regex = new RegExp(expression); var t = 'www.google.com'; if (t.match(regex)) { alert("Successful match"); } else { alert("No match"); }