如何将数字与紧跟在之后或之前的字母匹配


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());

输出应如下所示:

input = "2A Smith Road"
input = "333 Flathead Lake Road, Apartment 3B"

我认为寻找单词字符(w),前面((?<=...))是单词边界(b)和可选的数字(d*)应该涵盖所有情况:

const input = [
"2a smith road",
"333 flathead lake road, apartment 3b"
];
const capitalize = (s) => s.replace(/(?<=bd*)(w)/g, l => l.toUpperCase());
input.forEach(s => console.log(capitalize(s)))

您可以匹配 1 位或多个数字,后跟小写字符bd+[a-z]b并大写整个匹配项

[
"2a smith road",
"333 flathead lake road, apartment 3b"
].forEach(s => console.log(s.replace(/bd+[a-z]b/g, m => m.toUpperCase())));

相关内容

  • 没有找到相关文章

最新更新