如何删除字符串中的“Dr”,“Dr。”或“dr”?

"Dr. Drake Cohen" would become "Drake Cohen"
"Dr Drake Cohen" would become "Drake Cohen"
"Drake Cohen" would become "Drake Cohen"
"Rachel Smith" would become "Rachel Smith"
"Dr. Rachel Smith" would become "Rachel Smith"

我尝试过的
str.replace(/dr./i, "")

但这会删除Dr的所有实例

最佳答案

使用此正则表达式。

正则表达式:不区分大小写的 dr[\.\s]应该为ON。

Regex101 Demo

javascript - JavaScript正则表达式删除完全匹配的任何前缀-LMLPHP

从revo的评论来看,最好避免在名称中匹配dr,例如Alexandr

正则表达式: \bdr[.\s]\s?这将在word boundary之前检查dr

Regex101 Demo

关于javascript - JavaScript正则表达式删除完全匹配的任何前缀,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43664143/

10-09 17:28