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
1 Answer
Reset to default 7Go 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