最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript regex remove any prefix on perfect match - Stack Overflow

programmeradmin3浏览0评论

How can I remove "Dr", "Dr.", or "dr" in a string?

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

What I've tried:

str.replace(/dr./i, "")

but this removes all instances of Dr

How can I remove "Dr", "Dr.", or "dr" in a string?

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

What I've tried:

str.replace(/dr./i, "")

but this removes all instances of Dr

Share asked Apr 27, 2017 at 17:16 user2954587user2954587 4,8717 gold badges52 silver badges105 bronze badges 1
  • .replace(/^[dr\.]+ /gi, '') – Arkadi Commented Apr 27, 2017 at 17:20
Add a ment  | 

1 Answer 1

Reset to default 7

Go with this regex.

Regex: dr[\.\s] case insensitive should be ON.

Regex101 Demo


From revo's ment this one is far better to avoid matching dr in names such as Alexandr.

Regex: \bdr[.\s]\s? This will check for word boundary before dr.

Regex101 Demo

发布评论

评论列表(0)

  1. 暂无评论