我正在尝试更改一行中的所有斜杠以替换为每行beginnig中的3个字符的块。 (以下示例中的PMC,PAJ等)

PMC .89569XX / 90051XX / 90204XX / 89533XX / 90554XX / 90053XX / 90215XX / 89874XX / 89974XX / 90481XX / 90221XX / 90508XX / 90183XX / 88526XX / 89843XX / 88041XX / 90446XX / 88515XX / 89574XX / 89847XX / 88616 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 / 22070 22487XX / 22725XX.T00

理想的结果应该是:

PMC.89569XXPMC90051XXPMC90204XXPMC89533XXPMC90554XXPMC90053XXPMC90215XXPMC89874XXPMC89974XXPMC90481XXPMC90221XXPMC90508XXPMC90183XXPMC88526XXPMC89843XXPMC88041XXPMC90446XXPMC1515PM16CPMXXCPMCXCXXTPMCXCXCXCXCXCXCXCXCJCJTJCJTJTJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ的(的中文)

我不确定如何做到这一点。
这是我到目前为止所拥有的:

 (.)([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"));
    }

    09-06 21:00