gpt4 book ai didi

JavaScript 正则表达式删除完美匹配的任何前缀

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:30:38 24 4
gpt4 key购买 nike

如何删除字符串中的“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] 不区分大小写应该打开。

Regex101 Demo

enter image description here


从 revo 的评论来看,避免在 Alexandr 等名称中匹配 dr 要好得多。

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

Regex101 Demo

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com