Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby/Rails scan/match with regex from a tag to another out of text
    primarykey
    data
    text
    <p>In the following content samples, I wrapped the lines to make it easier to read on Stackoverflow (so you don't have to scroll to the right to look at the examples).</p> <p>Content A:</p> <pre><code>"Lorem Ipsum\r\n [img]http://example.org/first.jpg[/img]\r\n [img]http://example.org/second.jpg[/img]\r\n more lorem ipsum ..." </code></pre> <p>Content B:</p> <pre><code>"Lorem Ipsum\r\n [img caption="Sample caption"]http://example.org/third.jpg[/img] [img]http://example.org/fourth.jpg[/img]" </code></pre> <p>Content C:</p> <pre><code>"Lorem Ipsum [img]http://example.org/fifth.jpg[/img]\r\n more lorem ipsum\r\n\r\n [img caption="Some other caption"]http://example.org[/img]" </code></pre> <p>What I've tried:</p> <pre><code>content.match(/\[img\]([^&lt;&gt;]*)\[\/img\]/imu) return example: "[img]...[/img]\r\n[img]...[/img] content.scan(/\[img\]([^&lt;&gt;]*)\[\/img\]/imu) return example: "...[/img]\r\n[img]..." </code></pre> <p>What I would like to accomplish when running the scan/match/regex solution over the above 3 Content Examples is to get every occurence of <code>[img]...[/img]</code> and <code>[img caption="?"]...[/img]</code> and put it in an array for later use.</p> <pre><code>Array 1 : A : [img]http://example.org/first.jpg[/img] 2 : A : [img]http://example.org/second.jpg[/img] 3 : B : [img caption="Sample caption"]http://example.org/third.jpg[/img] 4 : B : [img]http://example.org/fourth.jpg[/img] 5 : C : [img]http://example.org/fifth.jpg[/img] 6 : C : [img caption="Some other caption"]http://example.org[/img] </code></pre> <p>It would also be helpful to limit the "stripped content" to only where there is an open and closign tag, meaning when there is a <code>[img]</code> / <code>[img caption="?"]</code> and a missing <code>[/img]</code> afterwards, to ignore it.</p> <p>I've read the <a href="http://www.ruby-doc.org/core-1.9.3/String.html" rel="nofollow">http://www.ruby-doc.org/core-1.9.3/String.html</a> up and down but can't find anything that seem to work for this.</p> <p>Update:</p> <p>So I figured that this:</p> <pre><code>\[img([^&lt;&gt;]*)\]([^&lt;&gt;]*)\[\/img\] </code></pre> <p>will find either:</p> <pre><code>[img]something[/img] </code></pre> <p>and:</p> <pre><code>[img caption="something"]something[/img] </code></pre> <p>Now I just need to know how to catch every occurence inside the different contents. I can always just get it from the first to the last [img][/img] tags, so when there is other Lorem Ipsum in between it will get grabbed too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload