代码之家  ›  专栏  ›  技术社区  ›  Bill

匹配模式,但不知道如何替换它们

  •  0
  • Bill  · 技术社区  · 6 年前

    我的绳子是

    test\<string1\>again\<string2\>blabla
    

    https://regex101.com/r/ZkIZvg/1

    const regex = /\\<[^<]*\\\>/g
    

    但是我怎么能删除多个 \< \>

    test<string1>again<string2>blabla
    

    所以 string1 string2 必须保持不变。

    我不能简单地替换 \>

    1 回复  |  直到 6 年前
        1
  •  2
  •   CertainPerformance    6 年前

    捕获 <

    const input = String.raw`test\<string1\>again\<string2\>blabla`;
    console.log(
      input.replace(/\\(<[^<]*)\\\>/g, '$1>')
    );
        2
  •  -1
  •   random    6 年前

    // Relace backSlash with the empty string
    const str = "test\<string1\>again\<string2\>blabla";
    console.log(str.replace(/\\/g, ''));