Skip to content

More readable regex#1

Open
sahithvibudhi wants to merge 3 commits into
imjenal:masterfrom
sahithvibudhi:master
Open

More readable regex#1
sahithvibudhi wants to merge 3 commits into
imjenal:masterfrom
sahithvibudhi:master

Conversation

@sahithvibudhi

Copy link
Copy Markdown

creating regex from string by using a common template.

changed

let urls = [ 
            /^http(s)?:\/\/(www\.)?([-a-zA-Z0-9@:%_\+.~#?&//=]*)adcb(india|netlink|careers)?\.com([-a-zA-Z0-9@:%_\+.~#?&//=]*)/, 
            /^https:\/\/(www\.)?([-a-zA-Z0-9@:%_\+.~#?&//=]*)americanexpress\.com([-a-zA-Z0-9@:%_\+.~#?&//=]*)/
];
for(let i =0; i<urls.length; i++) {
    if(urls[i].test(url)) {
        return true;
    }
}

To

let urls = [ 
            "adcb(india|netlink|careers)?.com", 
            "americanexpress.com"
];
for(let i =0; i<urls.length; i++) {
    let re = new Regex(`^http(s)?:\/\/(www\.)?([-a-zA-Z0-9@:%_\+.~#?&//=]*)${urls[i]}([-a-zA-Z0-9@:%_\+.~#?&//=]*)`);
    if(urls[i].test(url)) {
        return true;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant