My task is to check if string of an arbitrary length contains any latin letter except specific ones (in my case letters that match roman numerals (I,V,X,L,C) are not allowed)
So if no latin letter is present - the string is invalid, if specific character (mentioned above) is present - the string is invalid. Otherwise the string is valid.
Could someone help me to come up with proper regex for that condition?
Examples:
123 - invalid
123 aAA - valid
ABD - valid
AAa ddDD zzZz - valid
AXXXA - invalid
IV abc - invalid
This is a standard RegEx requirement, build a character class of all the characters you want to match. From your question this RegEx should do it for you: /^[^CDILMXV]*(?:[ABE-JKN-UWY-Z])+[^CDILMXV]*$/i
Note: I am allowing for more roman numerals than you had in your post, if that.s not what you need adjust accordingly.
The RegEx must have at least one allowed latin character (the middle part (?:[ABE-JKN-UWY-Z])+ and there must be no disallowed latin characters before or after the middle match (the [^CDILMXV]* at the start and end)
This should match all of your inputs.
This should be it. First part (?=.*[a-z]) checks for latin letters, and [^XLVCI] mathes anything besides these characters.
let string1 = '123'
let string2 = '123 aAA'
let string3 = 'ABD'
let string4 = 'AAa ddDD zzZz'
let string5 = 'AXXXA'
let string6 = 'IV abc'
let regex = /^(?=.*[a-zA-Z])[^XLVCI]+$/
let result = string1.match(regex)
console.log(string1.match(regex))
console.log(string2.match(regex))
console.log(string3.match(regex))
console.log(string4.match(regex))
console.log(string5.match(regex))
console.log(string6.match(regex))