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

按不在特定符号之间的符号拆分字符串

  •  1
  • mr__brainwash  · 技术社区  · 6 年前

    它的工作原理如下:

    <%qwr<%qrw<%tret%>wet%>qwt => only this is scpecial <%tret%>
    <%test142%>wqr%>%<%%>qwr%> => only this is <%test142%> and <%%> is special
    

    1) my&string=21<%253&124%> <&> && => ['my', 'string=21<%253&124%> <', '> ', '', '']
    
    2) new<%<&%235<%test&gg%>&test&f => ['new<%<', '%235<%test&gg%>', 'test', 'f']
    
    3) a&<%&qwer&>ty%>&af => ['a', '<%&qwer&>ty%>', 'af']
    

    我试过了 '\&(?![^<%]*%>)' (?<!(<%))\&(?!(%>)) 但那是错误的。

    1 回复  |  直到 6 年前
        1
  •  1
  •   d-h-e    6 年前

    我需要一个变通办法。

    1. 全部匹配 <% & %> _ 下划线)因此结果是 <% _ %>

    2. & 烧焦

    3. 最后替换特殊字符 回到 &

    const mySplit = (mystr) => {
      const regex = /<%(?!%>).*%>/gm;
      const matches = mystr.match(regex);
    
      const tmpreplace = matches.map(e => e.replace(/&/g,'_'));
      matches.forEach(e => mystr = mystr.replace(e,tmpreplace));
    
      return mystr.split('&').map(e => e.replace(/_/g,'&'));
    }
    
    console.log(mySplit('my&string=21<%253&124%> <&> &&'));
    console.log(mySplit('new<%<&%235<%test&gg%>&test&f'));
    console.log(mySplit('a&<%&qwer&>ty%>&af'));