JavaScript正则表达式匹配文本字段中的URL [英] JavaScript Regex to match a URL in a field of text
问题描述
如何设置我的正则表达式以进行测试,以查看javascript中的文本块中是否包含URL。我无法弄清楚用来完成这个的模式
How can I setup my regex to test to see if a URL is contained in a block of text in javascript. I cant quite figure out the pattern to use to accomplish this
var urlpattern = new RegExp( "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"
var txtfield = $('#msg').val() /*this is a textarea*/
if ( urlpattern.test(txtfield) ){
//do something about it
}
编辑:
所以我现在在正则表达式测试人员中使用的模式我需要它做什么但是chrome会抛出错误
So the Pattern I have now works in regex testers for what I need it to do but chrome throws an error
"Invalid regular expression: /(http|ftp|https)://[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#]*[w-@?^=%&/~+#])?/: Range out of order in character class"
$ b $的范围乱序b
以下代码:
for the following code:
var urlexp = new RegExp( '(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?' );
推荐答案
虽然逃避破折号字符(在字符类中可以具有特殊含义作为字符范围说明符) 应该工作,另一种去掉它们的特殊含义的方法是将它们放在类定义的开头或结尾。
Though escaping the dash characters (which can have a special meaning as character range specifiers when inside a character class) should work, one other method for taking away their special meaning is putting them at the beginning or the end of the class definition.
此外, \ +
和 \ @
在一个字符类中,JavaScript引擎分别被解释为 +
和 @
;然而,逃避并不是必要的,可能会让某些人试图在视觉上解释正则表达式。
In addition, \+
and \@
in a character class are indeed interpreted as +
and @
respectively by the JavaScript engine; however, the escapes are not necessary and may confuse someone trying to interpret the regex visually.
我会为你的目的推荐以下正则表达式:
I would recommend the following regex for your purposes:
(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?
这可以通过将其传递给RegExp构造函数在JavaScript中指定(就像在示例中所做的那样) ):
this can be specified in JavaScript either by passing it into the RegExp constructor (like you did in your example):
var urlPattern = new RegExp("(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?")
或直接指定正则表达式文字,使用 //
报价方法:
or by directly specifying a regex literal, using the //
quoting method:
var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/
如果接受正则表达式作为字符串(例如,来自用户输入或AJAX调用),则需要RegExp构造函数,并且可能更具可读性(在本例中)。我相当肯定 //
引用方法更有效,并且在某些时候更具可读性。两者都有效。
The RegExp constructor is necessary if you accept a regex as a string (from user input or an AJAX call, for instance), and might be more readable (as it is in this case). I am fairly certain that the //
quoting method is more efficient, and is at certain times more readable. Both work.
我在< JSFiddle >和< RegexLib.com >,使用客户端 - 正面的正则表达式引擎(浏览器),特别是选择JavaScript。虽然第一个失败了你说的错误,我建议的修改成功。如果我从源代码中的 http
中删除 h
,它将无法匹配,因为它应该!
I tested your original and this modification using Chrome both on <JSFiddle> and on <RegexLib.com>, using the Client-Side regex engine (browser) and specifically selecting JavaScript. While the first one fails with the error you stated, my suggested modification succeeds. If I remove the h
from the http
in the source, it fails to match, as it should!
如@noa在评论中所述,上述表达式与本地网络(非互联网)不匹配服务器或使用单个单词访问的任何其他服务器(例如 http:// localhost /
...或 https:// sharepoint-test-server /
...)。如果需要匹配此类型的网址(可能是也可能不是),以下内容可能更合适:
As noted by @noa in the comments, the expression above will not match local network (non-internet) servers or any other servers accessed with a single word (e.g. http://localhost/
... or https://sharepoint-test-server/
...). If matching this type of url is desired (which it may or may not be), the following might be more appropriate:
(http|ftp|https)://[\w-]+(\.[\w-]+)*([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?
#------changed----here-------------^
< 结束编辑>
最后,这是一个教会我的优秀资源90我所知道的关于正则表达式的百分比是 Regular-Expressions.info - 如果你想要我强烈推荐它学习正则表达式(它可以做什么,不能做什么)!
Finally, an excellent resource that taught me 90% of what I know about regex is Regular-Expressions.info - I highly recommend it if you want to learn regex (both what it can do and what it can't)!
这篇关于JavaScript正则表达式匹配文本字段中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!