totothegreat
totothegreat

Reputation: 1643

Regular expression for a url with no www and with .html in the end?

I have this url:

http://thisjs.blogspot.co.il/2015/01/blog-post_7.html

And in javascript i have this pattern to check for it's validity, which returns false for some reason, what is missing here?

var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
            '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
            '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
            '(\\:\\d+)?(\\/[-a-z\d%_.~+#!]*)*'+ // port and path
            '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
            '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
            if(!pattern.test(str)) {
                return false;
            } else {
                return true;
            }

I want true on the url i mentioned above? where do i look first?

Upvotes: 0

Views: 40

Answers (1)

anubhava
anubhava

Reputation: 785108

You need to have:

'(\\:\\d+)?(\\/[-a-z\\d%_.~+#!]*)*'+ // port and path

i.e. \\d instead of \d otherwise you are just matching literal d instead of a digit.

Upvotes: 1

Related Questions