简体   繁体   English

如何在此正则表达式中限制逗号?

[英]How to restrict comma in this regex?

I am using this regex to match URLs: 我正在使用此正则表达式来匹配URL:

^((http|https|ftp)://)?(([\w-]+\.)+[^#?\s]+)(.*)?(#[\w\-]+)?$

Now, I want to disable comma in this regex. 现在,我想在此正则表达式中禁用逗号。

In order to disallow commas in your regex, just exclude it using negated character classes: 为了禁止在正则表达式中使用逗号,只需使用否定的字符类将其排除:

^((http|https|ftp)://)?(([\w-]+\.)+[^,#?\s]+)([^,]*)?(#[\w-]+)?$
                                    ^^        ^^^^^

See demo 观看演示

Note how I replaced .* with [^,]* which means match 0 or more characters other than a comma . 请注意,我是如何用[^,]*替换.* ,这意味着要匹配0个或多个除逗号以外的字符 This way you can further adjust the regex including more characters that you do not want to see inside your URLs. 这样,您可以进一步调整正则表达式,包括不想在URL中看到的更多字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM