gpt4 book ai didi

javascript - 用同一行开头的匹配项替换一行中的所有斜杠

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

我正在尝试更改一行中的所有斜线,以替换为每行开头的 3 个字符 block 。 (下面示例中的 PMC、PAJ 等)

.PMC.89569XX/90051XX/90204XX/89533XX/90554XX/90053XX/90215XX/89874XX/89974XX/90481XX/90221XX/90508XX/90183XX/88526XX/89843XX/XX99516XX/804416XX/90843XX/884416XX/9/89847XX/88616XX/90513XX/90015XX/90334XX/89649XX.T00
.PAJ.77998XX/77896XX.T00.PAG.78116XX/78104XX/77682XX/07616XX/77663XX/77863XX/07634XX/78088XX/77746XX/78148XX.T00.PKC.22762XX/22358XX/22055XX/22672XX/22684XX/22154XX/22608XX/22768XX/22632XX/22266XX/22714XX/22658XX/22631XX/22288XX/22020XX/22735XX/22269XX/22138XX/22331XX/22387XX/22070XX/22636XX/22629XX/22487XX/22725XX.T00

期望的结果应该是:

PMC.89569XXPMC90051XXPMC90204XXPMC89533XXPMC90554XXPMC90053XXPMC90215XXPMC89874XXPMC89974XXPMC90481XXPMC90221XXPMC90508XXPMC90183XXPMC88526XXPMC89843XXPMC88041XXPMC90446XXPMC88515XXPMC89574XXPMC89847XXPMC88616XXPMC90513XXPMC90015XXPMC90334XXPMC89649XX.T00

我不确定如何完成此操作。这是我目前所拥有的:

 (.)([A-Z]{3})(.)(\/)

最佳答案

如果您只打算支持 ECMAScript 2018 及更新版本,您可以使用单个正则表达式实现所需的功能:

.replace(/(?<=^\.([^.]+)\..*?)\//g, "$1")

参见 regex demo .

详情

  • (?<=^\.([^.]+)\..*?) - 在当前位置的左侧立即进行正面观察,需要
    • ^ - 字符串开始
    • \. - 一个点
    • ([^.]+) - 第 1 组:除点之外的一个或多个字符
    • \. - 一个点
    • .*? - 任何 0+ 个字符,除换行字符外,尽可能少
  • \/ - 一个 /字符。

JS 演示:

var strs = ['.PMC.89569XX/90051XX/90204XX/89533XX/90554XX/90053XX/90215XX/89874XX/89974XX/90481XX/90221XX/90508XX/90183XX/88526XX/89843XX/88041XX/90446XX/88515XX/89574XX/89847XX/88616XX/90513XX/90015XX/90334XX/89649XX.T00','.PAJ.77998XX/77896XX.T00','.PAG.78116XX/78104XX/77682XX/07616XX/77663XX/77863XX/07634XX/78088XX/77746XX/78148XX.T00','.PKC.22762XX/22358XX/22055XX/22672XX/22684XX/22154XX/22608XX/22768XX/22632XX/22266XX/22714XX/22658XX/22631XX/22288XX/22020XX/22735XX/22269XX/22138XX/22331XX/22387XX/22070XX/22636XX/22629XX/22487XX/22725XX.T00'];
for (var s of strs) {
console.log(s.replace(/(?<=^\.([^.]+)\..*?)\//g, "$1"));
}

关于javascript - 用同一行开头的匹配项替换一行中的所有斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53904952/

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