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 を返す必要があります:

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 . 
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 に答える