0

I'm trying to do a script who extract tags from a string
For that, i used RegExp
I simplified my code as much as possible.

 var ereg = /(\[text\](.+)\[\/text\])+?/;
 var str = '[text]MyText[/text][text]MyOtherText[/text]';
 result = ereg.exec(str);

 if (result) {
    for (i = 0; i < result.length; i++) {
        document.body.innerHTML += "<br>[" + i + "] => " + result[i];
     }
 }

Output:

 [0] => [text]MyText[/text][text]MyOtherText[/text]
 [1] => [text]MyText[/text][text]MyOtherText[/text]
 [2] => MyText[/text][text]MyOtherText     

Debuggex Demo
jsFiddle Demo

My problem is that i want the variable result return me what is between the tags [text][/text], meaning 'MyText' et 'MyOtherText'.
Instead it returns me 'MyText ** [/ text] [text] MyOtherText **'. How do I do this?
My regular expression is not non-greedy?

Lemarcque
  • 9
  • 4
  • Make the repetition lazy `(.+?)`. – ndnenkov Dec 29 '15 at 17:40
  • If you are not doing this for interest/practise sake then you may want to look at a parser library, I have found this one quite small and useful in the past https://github.com/patorjk/Extendible-BBCode-Parser And of course it depends on the complexity of the BBcode that you need to parse. An example that I gave in another question http://stackoverflow.com/questions/28383102/regex-doesnt-stop-matching/28384474#28384474 – Xotic750 Dec 29 '15 at 17:47
  • 1
    @ndn Thank you it is working ! – Lemarcque Dec 30 '15 at 03:22
  • @Xotic750 Thank you, yes, i'll try to do something with it. – Lemarcque Dec 30 '15 at 03:26
  • @Lemarcque,happy to help. – ndnenkov Dec 30 '15 at 06:15

0 Answers0