0

I need to validate whether a given string is a valid mDNS address. Examples of these are listed here:

a2aadd61-85d9-4be6-a36c-267ad65917d3.local
ccd58700-cdd5-4c1f-8098-5bc306f1ce77.local
bb4115cd-1f39-42b0-9433-e59533c9c771.local
3b382824-24bf-457a-a7d6-b6e25e4d79bf.local
9284cbaa-2933-45e3-9ae2-c0cccae54b68.local
619281fb-6426-44fa-8d0f-9e58ce415e3e.local
541d8a99-b1a5-46f4-96f1-e31f30b3cb99.local
2feb782f-ea40-4a37-b843-5b5b4fe1860e.local

Is this possible? I am new to regex and can't seem to find any regex examples for this particular string. This was my attempt at making a regex test:

let valid = 'a2aadd61-85d9-4be6-a36c-267ad65917d3.local';
let invalid = 'not-valid-233q4q22eqwr-q3rhHE02n333r4tsefs.local';

let regex = \([a-zA-Z1-9]+){8}-([a-zA-Z1-9]){4}-([a-zA-Z1-9]){4}-([a-zA-Z1-9]){12}\.local\;

regex.test(valid)

Edit: I tried this on regex101.com and it didn't work. Any help? THanks!

SOLUTIONS:

I got it to work using this:

let valid = 'a2aadd61-85d9-4be6-a36c-267ad65917d3.local';
let invalid = 'not-valid-233q4q22eqwr-q3rhHE02n333r4tsefs.local';

let regex = /(^[a-zA-Z1-90]{8})-([a-zA-Z1-90]{4})-([a-zA-Z1-90]{4})-([a-zA-Z1-90]{4})-([a-zA-Z1-90]{12})\.local/;

console.log(regex.test(valid)) //=> true
console.log(regex.test(invalid)) //=> false

Thanks!

divinelemon
  • 1,216
  • 14
  • It looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Jan 27 '21 at 18:45
  • @WiktorStribiżew I attempted to make my own, can't seem to get it to work. Thanks :) – divinelemon Jan 27 '21 at 19:20
  • Good job, so the links proved helpful. Still, `1-90` is a bit convoluted, use `0-9` or just `\d`. – Wiktor Stribiżew Jan 27 '21 at 22:14
  • @WiktorStribiżew Thanks so much! If you would like to clean it up, please post it as an answer. Thanks again! – divinelemon Jan 27 '21 at 23:04

0 Answers0