2

In my code I have this type of text:

<h1>{{text}}</h1>
<p>{{other text}}</p>

How to get with with jQuery, those texts ?

-

My first idea was:

$(regex).each(function( index ) {
  alert(index); // must return {{text}}
}

1 Answers1

0

You're using each incorrectly:

$(regex).each(function() {
  alert($(this).html()); // must return {{text}}
});

Demo

Note that in the demo I used body * as the selector. jQuery doesn't take regex selectors, so you will need to write a proper selector.

Also note that your original code was missing ); at the end; I have added it.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Thanks for pointing me this. But how can I match the {{text}} ? –  Dec 15 '14 at 19:29
  • See my demo link. As long as `regex` contains a selector that matches the elements in question, the above will work. – elixenide Dec 15 '14 at 19:30
  • The problem is if I have `

    aaaaaa

    `, it will match it whereas this text is not between `{{` and `}}`.
    –  Dec 15 '14 at 19:36
  • `body *` matches everything in a subelement of body. You can do `body,body *` if you want anything in body itself or in a subelement. – elixenide Dec 15 '14 at 19:36
  • You need to filter for text matching `\{\{[^{}]+\}\}` inside the `each` function. Don't try to do this with the selector itself, or you will run into problems. – elixenide Dec 15 '14 at 19:37