Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex for comma separated URL

How can I validate the comma separated urls in a way that accepts subdomains, localhost and allows whitespace before or after the comma? Here’s an example of how I’d like this to work:

const regEx = regex; // working regular expression here
console.log(regEx.test('localhost:3000')); // should return true
console.log(regEx.test('https://google.com,https://jeith.com')); // should return true
console.log(regEx.test('subdomain.jeith.com, localhost:3000')); // should return true
console.log(regEx.test('jeith com')); // should return false because of whitespace not near comma
console.log(regEx.test('jeith.com,,localhost:3000')); // should return false because of double comma

I have an input requesting URLs separated by commas. Usually for something like this, I would split the string by commas and perform the url validation on the individual URLs, but I’m unable to do in this project as I’m doing the validation via zod.

This doesn’t need to be very strict, and validation for .com, .net, http://, ect isn’t needed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m a beginner with regex and this is closest I can get to this validation: /^[A-Za-z]+(?:\s*,\s*[A-Za-z]+)*$/. What’s missing in this expression is the ability to accept : and . for https:// and .com. Other than that, it is successfully returning false if they’re double commas present or whitespace away from the commas.

>Solution :

Take a look at the link below, she already does this and presents you step by step.

RegExr: validation url:

[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading