gpt4 book ai didi

javascript - 匹配模式的正则表达式

转载 作者:行者123 更新时间:2023-11-30 17:55:15 25 4
gpt4 key购买 nike

我有一个需求

.domain
.domain.com
.domain.com/path
.domain.com:443/path
domain.com
domain.com/path
domain.com:443/path
domain
/path
:443/path

应该都是真的

不过

.
./path
.:443
*.domain
*.domain/path
*.domain:443/path
/
:443
*/path
anything with ? in it

应该都返回 false。

我在 JS 中想出了以下内容。但是,经过一些修改后,我完全迷失了..

/^\.?[^:\/]([\da-z\.-]+)\.?([a-z\.]{2,6})?(:[0-9]+)?([\/\w \.-]*)*\/?$/ 

我的测试 stub 如下所示:

console.log(urlRegExp('*.domain/path'));
console.log(urlRegExp('*.domain:443/path'));
console.log(urlRegExp('/'));
console.log(urlRegExp(':443'));
console.log(urlRegExp('*/path'));
console.log(urlRegExp('domain.com?q=a'));

function urlRegExp(){
return /^\.?[^:\/]([\da-z\.-]+)\.?([a-z\.]{2,6})?(:[0-9]+)?([\/\w \.-]*)*\/?$/.test(arguments[0]) + " " + arguments[0];
}

我无法使用此模式处理所有列出的字符串。每当我更改某些内容以使某些内容通过时,另一个字符串就会失败。我是初学者,刚刚开始通过食谱深入了解 RegEx。但是,这可能需要一段时间,我需要尽快完成。任何帮助或指导都会很棒。

当前结果:

应该返回真:

true .domain
true .domain.com
true .domain.com/path
true .domain.com:443/path
true domain.com
true domain.com/path
true domain.com:443/path
true domain
false /path -- should be true
false :443/path -- should be true

应该返回错误:

false . 
false ./path
false .:443
true *.domain -- should be false
true *.domain/path -- should be false
true *.domain:443/path -- should be false
false /
false :443
false */path
false domain.com?q=a

最佳答案

也许是这样的……

/^[./:a-z]([0-9]+\/)?[a-z]+[^?]*$/

这里解释...

/^                 # Start regex, and start matching
[./:a-z] # starts with dot, slash, colon or a-z
([0-9]+\/)? # optionally has multi-digit number followed by slash
[a-z]+ # has one or more letters next
[^?]* # has zero or more characters that are not `?`
$/ # end of matching and end regex

很难知道你的具体目标是什么

测试...

CoffeeScript

rex = /^[./:a-z]([0-9]+\/)?[a-z]+[^?]*$/

for str in """
.domain
.domain.com
.domain.com/path
.domain.com:443/path
domain.com
domain.com/path
domain.com:443/path
domain
/path
:443/path
""".split /[\r\n]+/
console.log "Should be true - is #{if str.match rex then 'true ' else 'false'} #{str}"

for str in """
.
./path
.:443
*.domain
*.domain/path
*.domain:443/path
/
:443
*/path
domain.com?
domain.?com
?domain.com
""".split /[\r\n]+/
console.log "Should be false - is #{if str.match rex then 'true ' else 'false'} #{str}"

在...

Javascript

var rex, str, _i, _j, _len, _len1, _ref, _ref1;

rex = /^[./:a-z]([0-9]+\/)?[a-z]+[^?]*$/;

_ref = ".domain\n.domain.com\n.domain.com/path\n.domain.com:443/path\ndomain.com\ndomain.com/path\ndomain.com:443/path\ndomain\n/path\n:443/path".split(/[\r\n]+/);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
str = _ref[_i];
console.log("Should be true - is " + (str.match(rex) ? 'true ' : 'false') + " " + str);
}

_ref1 = ".\n./path\n.:443\n*.domain\n*.domain/path\n*.domain:443/path\n/\n:443\n*/path\ndomain.com?\ndomain.?com\n?domain.com".split(/[\r\n]+/);
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
str = _ref1[_j];
console.log("Should be false - is " + (str.match(rex) ? 'true ' : 'false') + " " + str);
}

两者输出相同...

Should be true - is true  .domain
Should be true - is true .domain.com
Should be true - is true .domain.com/path
Should be true - is true .domain.com:443/path
Should be true - is true domain.com
Should be true - is true domain.com/path
Should be true - is true domain.com:443/path
Should be true - is true domain
Should be true - is true /path
Should be true - is true :443/path
Should be false - is false .
Should be false - is false ./path
Should be false - is false .:443
Should be false - is false *.domain
Should be false - is false *.domain/path
Should be false - is false *.domain:443/path
Should be false - is false /
Should be false - is false :443
Should be false - is false */path
Should be false - is false domain.com?
Should be false - is false domain.?com
Should be false - is false ?domain.com

关于javascript - 匹配模式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160513/

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