Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript正则表达式在完全匹配时删除任何前缀_Javascript_Regex - Fatal编程技术网

JavaScript正则表达式在完全匹配时删除任何前缀

JavaScript正则表达式在完全匹配时删除任何前缀,javascript,regex,Javascript,Regex,如何删除字符串中的“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" 我所尝试

如何删除字符串中的“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实例,并使用此正则表达式

Regex:
dr[\.\s]
不区分大小写应处于启用状态


从revo的评论来看,这一点更好,避免在
Alexandr
等名称中匹配
dr

Regex:
\bdr[.\s]\s?
这将在
dr
之前检查
单词边界


几乎没有改进-
dr[\.\s]\s?
@Kinduser:dr后面是否有可选空格?@Kinduser
dr[\.\s]\s*
更好。@ibrahimmahrir是的,
*
更好。甚至
dr[.\s]+
@ibrahimmahrir
.replace(/^[dr\.]+/gi')