const input = "2a smith road ";
const input 2 = "333 flathead lake road, apartment 3b"
const address = input.replace(/(^\w{1})|(\s \w{1})/g, letter => letter.toUpperCase());
Output should look like this:
input = "2A Smith Road"
input = "333 Flathead Lake Road, Apartment 3B"
CodePudding user response:
I think looking for a word character (\w
), preceded ((?<=...)
) by a word boundary (\b
) and optional digits (\d*
) should cover all cases:
const input = [
"2a smith road",
"333 flathead lake road, apartment 3b"
];
const capitalize = (s) => s.replace(/(?<=\b\d*)(\w)/g, l => l.toUpperCase());
input.forEach(s => console.log(capitalize(s)))
CodePudding user response:
You can match 1 or more digits followed by a lower case char \b\d [a-z]\b
and uppercase the whole match
[
"2a smith road",
"333 flathead lake road, apartment 3b"
].forEach(s => console.log(s.replace(/\b\d [a-z]\b/g, m => m.toUpperCase())));