JavaScript regex remove any prefix on perfect match - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744861195a4597711.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信