-1

I have a URL from google circles that doesn't get validated by normal regular expressions. for instance, asp.net provides a standard regular expression to cope with URLS, which is:

"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"

But when you get a google circles URL:

https://plus.google.com/photos/114197249914471021468/albums/5845982797151575009/5845982803176407170?authkey=CKfNzLrhmenraA#photos/114197249914471021468/albums/5845982797151575009/5845982803176407170?authkey=CKfNzLrhmenraA

it can't cope.

I thought of appending to the end the following expression: (\?.+)? which basically means the URL can have a question mark after it and then any number of characters of any type, but that doesn't work.

The whole expression would be:

"[Hh][Tt][Tt][Pp]([Ss])?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*(\?.+)?)?"

For some reason, that doesn't work with complicated URLs either. Help is appreciated.

4

1 に答える 1

0

私はアンカーを追加し、^この$テストの目的の/ために、次は javascript の正規表現リテラルであるためをエスケープし、&そこにビジネスがなかった をに変更し&;、スペースを削除#し、3 番目の文字セットに追加しました。正常に動作します:

/^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w.\/?%&;#=-]*)?$/.test( 
    'https://plus.google.com/photos/114197249914471021468/albums/5845982797151575009/5845982803176407170?authkey=CKfNzLrhmenraA#photos/114197249914471021468/albums/5845982797151575009/5845982803176407170?authkey=CKfNzLrhmenraA' )   
// true

-また、範囲を指定しない場合はセットの最初または最後にあるはずなので、3 番目の文字セットの最後に を移動しました。

免責事項: これは一般的に URL を検証する良い方法として提案するものではありません。これは、この特定のケースで機能する元の正規表現を編集したものにすぎません。

于 2013-02-18T18:46:18.483 に答える