1

我有一个要求

.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 \.-]*)*\/?$/ 

我的测试存根如下所示:

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
4

1 回答 1

2

也许像这样的东西......

/^[./: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

很难知道你的目标是什么

测试在...

咖啡脚本

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
于 2013-08-10T09:46:45.907 回答