const regex = /\A\s*
(?: #########################################################################
# Matches against Australian postal addresses
# [<floor> <number>]
# [<unit type>] [<unit>] <house number> <street name> <street type>
# [<anything trailing>
#########################################################################
(?:(?P<c_level_type>level|lvl|floor|flr)\s*(?P<c_level_number>\w+),?\s*)? # Level (floor, etc)
\s*,?\s* # Optional comma
(?:(?P<c_unit_type>[[:alpha:]][\w-']*)?\s*)? # Type (suite, unit, etc)
(?:(?P<c_unit_number>[\w\.]*(?!\d))\s*[,\/]*\s*)? # Unit
(?P<c_house_number>\pN+[[:alpha:]]?(?:\s*[-\/\pP]\s*\pN+[[:alpha:]]?)*) # House number
\s*,?\s* # Optional comma
(?P<c_street_name>[\w-\s]*?) # Street name
\s+
\b(?P<c_street_type>fire\s*track|right\s*of\s*way|service\s*way|shopping\s*centre|state\s*highway|\w{2,})\b # Street type
(?:\s*\R+(?P<c_trailing>.+))? # Trailing data not captured
)
\s*\Z/uigs;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\A\\s*
(?: #########################################################################
# Matches against Australian postal addresses
# [<floor> <number>]
# [<unit type>] [<unit>] <house number> <street name> <street type>
# [<anything trailing>
#########################################################################
(?:(?P<c_level_type>level|lvl|floor|flr)\\s*(?P<c_level_number>\\w+),?\\s*)? # Level (floor, etc)
\\s*,?\\s* # Optional comma
(?:(?P<c_unit_type>[[:alpha:]][\\w-\']*)?\\s*)? # Type (suite, unit, etc)
(?:(?P<c_unit_number>[\\w\\.]*(?!\\d))\\s*[,\\\/]*\\s*)? # Unit
(?P<c_house_number>\\pN+[[:alpha:]]?(?:\\s*[-\\\/\\pP]\\s*\\pN+[[:alpha:]]?)*) # House number
\\s*,?\\s* # Optional comma
(?P<c_street_name>[\\w-\\s]*?) # Street name
\\s+
\\b(?P<c_street_type>fire\\s*track|right\\s*of\\s*way|service\\s*way|shopping\\s*centre|state\\s*highway|\\w{2,})\\b # Street type
(?:\\s*\\R+(?P<c_trailing>.+))? # Trailing data not captured
)
\\s*\\Z', 'uigs')
const str = `level 2, 100 smith avenue`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions