/    Sign up×
Community /Pin to ProfileBookmark

Regular expression for URL

Hello,

Please, help me to fix this regular expression to make it workable in JavaScript:

[CODE]var r = /^((((ht|f)tp(s?))://)|(/{1}))?((([a-zA-Z0-9_-]{2,}.)+[a-zA-Z]{2,})|((?:(?:25[0-5]|2[0-4]d|[01]dd|d?d)(?(.?d).)){4}))(:[a-zA-Z0-9]+)?(/[a-zA-Z0-9-._?,’/\+&%$#=~]*)?$/;[/CODE]

In fact, [URL=”http://regexlib.com/(A(uFT8pIyZ9AmYA93z_oQH6oPf_n5uQiIkmCP3N7X-kPg-4RcuPGdgD0w1ZGPV7yTvS15sGVMIZNAUMcBGPaYXkyU-yPK333uo6sKn9hYDJuLQ3_K49h4kQDyk3w6J2wRSVXSdo1-ouuCcKwiSXIm-PCMKuJiN80hnXVOsC6RRbO_vU8qgUQw-4uJsR_i_h63v0))/RETester.aspx”]here[/URL] it works fine, but only if i choose .NET Regex Engine. When i’m using JavaScript one, it doesn’t even run.

What have i tried is to screen this place “…//…” with “…//…”, but it doesn’t help.

Thanks!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@007JulienDec 11.2012 — You have to escape no only the two first slashes but the followings...

It would be good to know what it has to make (http or ftp with a possible s but after ([COLOR="#FF0000"][/COLOR]/{1}) ???) rather than try to guess it !
Copy linkTweet thisAlerts:
@HolfistengauthorDec 11.2012 — Actually, i had next regexp, but extended it by myself for understanding relative paths like "/folder/other":
[CODE]^(((ht|f)tp(s?))://)?((([a-zA-Z0-9_-]{2,}.)+[a-zA-Z]{2,})|((?:(?:25[0-5]|2[0-4]d|[01]dd|d?d)(?(.?d).)){4}))(:[a-zA-Z0-9]+)?(/[a-zA-Z0-9-._?,'/\+&%$#=~]*)?$[/CODE]
Copy linkTweet thisAlerts:
@007JulienDec 11.2012 — I do not understand this regular expression.

I see four parts (I add [COLOR="#FF0000"][/COLOR] before the javascript delimiter / for regular expressions) :



1/ ^(((ht|f)tp(s?))[COLOR="#FF0000"][/COLOR]:/[COLOR="#FF0000"][/COLOR]/)?

2/ [COLOR="#00FF00"]([/COLOR](([a-zA-Z0-9_-]{2,}.)+[a-zA-Z]{2,})[COLOR="#00FF00"]|[/COLOR]((?: (?:25[0-5]|2[0-4]d|[01]dd|d?d)(?(.?d).)){4})[COLOR="#00FF00"])[/COLOR] with a [COLOR="#00FF00"]green[/COLOR] alternative

3/ (:[a-zA-Z0-9]+)?

4/ (/[a-zA-Z0-9-._?,'[COLOR="#FF0000"][/COLOR]/+&%$#=~]*)?$



The first one is optional ( )?

The second is made with

[LIST]
  • [*]at least two alphanumeric characters, followed by a point and at least two alphabetic characters

  • [*]or :[LIST]
  • [*]25 followed by a number included between 1 and 5,

  • [*]2 followed by a number included between 0 and 4 and a digit,

  • [*]0 or 1 followed by two digits,

  • [*]one or two digits,

  • [/LIST]
    followed by something like (?(.?d).) which make not sense the parenthesis cannot be optional. (?: ) would be a non-capturing sub-patterns ?

    //...

    The fourth part is also curious with a [I]amp[/I] in a class which contains all alphabetic characters...
    [/LIST]
    If you want to use an alternative with [I]/folder[/I] or [I]/other[/I] you have to specify the part to replace ...
    ×

    Success!

    Help @Holfisteng spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...