const regex = new RegExp('^(?:[A-Z] \\d|[^\\W\\d_]{2,}\\.?)(?:[- \\'’][^\\W\\d_]+\\.?)*$', 'gm')
const str = `########### Absolutly Fine ################
Mainstreet.
Mainstreet
Main Street
Big New mainstreet
Mainstreet-New
Mains Str.
St. Alexander Street
Übermorgen Straße
John Kennedy Street
Bahnhofstr.
Leonhard-Eck-Str.
Älterweg
Graf-Anton-Weg
Alexanderstraße
Prof.-Ernst-Nathan-Straße
Prof.-Albert-Einstein-Weg
Prof. Ernst-Nathan-Straße
Prof. Albert-Einstein-Weg
########## Not very common but fine!!! ###################
A 1
K 7
H 2
P 1
I 7
E 7
T 3
######## Not okay ################
A
B
Mainstreet #+;:_*´\`?=)(/&%\$§!
Mainstreet#+;:_*´\`?=)(/&%\$§!
Mainstreet 2
Mainstreet..
Mainstreet§
`;
// 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