Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
Regex: how to match a particular syntax when it doesn't include that particular syntax, single line?

I have a particular syntax I use in strings which helps me retrieve and insert substrings. an example:

{{ variable ||| <div>example text</div>}}

Now I find myself having something like:

{{ variable1 ||| 
    <div>some text</div>
  <ul>
    {{ variable2 |||<li>some other text<li/>}}
    {{ variable3 |||<li>even more text<li/>}}
  </ul>
}}

and obvously the regex I use for the first case does not match correctly.

the regex I usually use is:

/\B\{\{(.*?)\|\|\|(.*?)\}\}/

which in the second example matches:

{{ variable1 ||| 
    <div>some text</div>
  <ul>
    {{ variable2 |||<li>some other text<li/>}}

which is not what I want.

I need a regex to match that particular syntax when it doesn't containt that particular syntax. I've tried many combinations similar to this:

\B\{\{(.*?)\|\|\|(.*(?!\{\{.*?\|\|\|.*?\}\}).*)\}\}

but can't work it out. It would be enough to get only

{{ variable2 |||<li>some other text<li/>}}

Because after the match I can delete that syntax so that recursively checking I can match everything.

The difficulty I have is that the string I parse is only in one line, so I can't use anythng like

^(?!example).*$

Am I right? What am I missing? I couldn't find a solution in other questions so I wrote this; sorry if this is a duplicate.

Thank you very much

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem you have here is that you are starting to get past what you can usefully* achieve with a regex - the way things are going now, it looks like sooner or later you'll hit on something like:

{{ variable1 ||| 
    <div>some text</div>
  <ul>
    {{ variable2 |||<li>some other text<li/>}}
    {{ variable3 |||<li>even more text 
                        <ul>
                            <li>text always here</li>
                            {{ variable4 ||| <li>optional text</li>}}   
                        </ul>
                     <li/>}}
  </ul>
}}

In this kind of situation you've gone past the point where you have convenient text substitution and you're creating your own mark-up language and templating system. Once you are aware that is what you are doing, it is not too hard, but it's definitely more of a heavyweight task - you will want to parse your text into a tree (Regexes can be very useful for this) and then use your variables to decide which nodes to show.

The thing to think about if you get to this point is whether you are better off using your own mark-up for this, or whether there is an existing tool that can handle what you need already with minimal changes to your existing documents. There are various templating engines available in JavaScript ( I think Moustache and Handlebars are best known, but tools like JSRender might be worth a look ) and it is very possible that something matches your need already.


* When I say usefully what I mean is that beyond a certain point if you can solve a problem with a Regex it tends to become cumbersome and hard to maintain. Regular Expressions are great tools for simple text operations, but if they get too big or complicated it can be very hard to troubleshoot them.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda