How do I match a specific pattern and exclude specific sub-strings with regex case insensitive
I am trying to write Regex for New Zealand address validation. This is the valid character set which I want to capture case insensitive which includes A to Z numbers and letters hyphen "-" and forward slash "/" as well as Maori accented characters for Maori vowels ā, ē, ī, ō, ū and works.
....
var regex = /^[\/A-zĀ-ū0-9\s\,\''\-]*$/;
....
It needs to exclude the following sub-strings case insensitive, with or without spaces to be valid
PO Box
Private Bag
(tricky as both those sub-strings could include spaces or not and be upper or lower case depending on how the user types them)
and the string must start with a number to be valid
e.g.
This is invalid:
Flat 1 311 Point Chevalier Road, Point Chevalier, Auckland 1022, New Zealand
This is valid:
1/311 Point Chevalier Road, Point Chevalier, Auckland 1022, New Zealand
311/1 or 311-1 or 1-311 are all considered valid by NZ Postal Service.
example if this if statement is true considering the regex above then the address string is invalid:
// Allowed character set
var regex = /^[\/A-zĀ-ū0-9\s\,\''\-]*$/;
// Get the address string and convert to lowercase for evaluation pseudocode
var str = getValue().toLowerCase();
// Strip spaces
str = str.replace(/\s/g, '');
// If the sub string "pobox" or sub string "privatebag" or string doesn't start with a number or doesn't match allowed character set address is invalid
if(str.includes("pobox") || str.includes("privatebag") || (str.match(/^\d/) == null) || (!regex.test(str))){
....
Thanks I really appreciate the input of the community and I know there are Regex gurus out there. I am trying to simplify this so I can use HTML5 form validation rather than a clunky JavaScript evaluation.
^\d... for disallowing some condition at certain points in your pattern eg a neg. lookahead can be used. Together maybe something like this demo.