如何使用正则表达式删除字符串中最后一个字母字符后的任何非字母字符?

时间:2017-10-03 18:30:04

标签: javascript regex

所以我想申请清理我所拥有的字符串的规则是:

搜索最后一个字符,其中包含A-Z / i之间的字母,并删除其后的所有字符。像这样:

"WASHER," -> "WASHER" (R is last alpha character)
"VALVE,NEEDLE," -> "VALVE,NEEDLE" (E is last alpha character)
"HOSE,HYDRAULIC. 3466" -> "HOSE,HYDRAULIC" (C is last alpha character)
"WASHER . " -> "WASHER" (R is last alpha character)

1 个答案:

答案 0 :(得分:0)

我发现它,使用:[^ A-Z] + $

console.log("WASHER,".replace(/[^A-Z]+$/,""))
console.log("VALVE,NEEDLE,".replace(/[^A-Z]+$/,""))
console.log("HOSE,HYDRAULIC. 3466".replace(/[^A-Z]+$/,""))
console.log("WASHER . ".replace(/[^A-Z]+$/,""))