What does this short regular expression match?

/\[img\]([^[]*)\[\/img\]/ig 

This part is of interest: ([^ [] *)

As far as I can tell, the following are made up here:

[^[] - not equal to the symbol [ But everything else is allowed (why it is necessary, because in the bracket group there should be a url link to the image, for example.

* - greedy quantifier, is equal to zero or more characters (why is there zero? I think there should be a + )

 var reg2 = /\[img\]([^[]*)\[\/img\]/ig; var match2,text_str; while (match2 = reg2.exec(text)) { console.log(match2); } 
  • 2
    @Doofy, what nonsense? Each sentence has an error. 1. Not the beginning. He is in a character group. 2. The group is remembered. But in match2 it is not, but an array. 3. This is not a tag, but a bb-code. Square brackets. 4. The content of the bb-code is the url of the image. - Qwertiy

2 answers 2

why it is needed, because in the bracket group there should be a url image link

She will. There should be no square brackets in the url. Well, or they need to zaenkodit.

And what are the alternatives to determine the url? Search for denial [/img] ? Well you can, but why?

why is there zero? I think there should be +

For the correct address both options are the same. For incorrect ... Well ok.

  • Thank you very much. I could guess about it ... Eh ... I ... - gilo1212
  • А какие альтернативы для определения url? Here is - (.+) - gilo1212
  • @ gilo1212, [img]http://smth/ing1.png[/img] и [img]http://smth/ing2.png[/img] - p .+ get garbage - then too .+? . Now let's take the broken markup. Тег [img] позволяет сделать так: [img]http://smth/ing1.png[/img] - .+? devour everything from the first [img] to the end - this is also not done. - Qwertiy
  • .+ получится фигня In the index everything turns out fine, the links are written as needed. But the whole string is written to input. But we are looking for links, does it matter what is written to the input? Or will it be longer to move from the fact that the entire line is written every time? - gilo1212
  • @ gilo1212. Again. var s="[img]http://smth/ing1.png[/img] и [img]http://smth/ing2.png[/img]", r=/\[img\](.+)\[\/img\]/ig; Show me what code are you going to get the addresses of? - Qwertiy

The regular expression /\[img\]([^[]*)\[\/img\]/ig corresponds to this [img]тут что угодно кроме '[' символа[/img] .

  • i - Register-independent search.
  • g - Global search. Finds all matches.

([^[]*) - It stands for:

  1. [^[] - Find any character except [ .
  2. * - Matches zero or more [^[] expressions.
  3. () - Capture everything that falls under the expression in brackets.

* used to find such code [img][/img]