Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <a href="http://rubular.com/r/aQKZviwOzG" rel="nofollow"><code>/\[img(?:\s+caption=".+")?\].+?\[\/img\]/</code></a> to scan the documents:</p> <pre><code>regex = /\[img(?:\s+caption=".+")?\].+?\[\/img\]/ text = &lt;&lt;EOT Lorem Ipsum [img]http://example.org/first.jpg[/img] [img]http://example.org/second.jpg[/img] more lorem ipsum ... Content B: Lorem Ipsum [img caption="Sample caption"]http://example.org/third.jpg[/img] [img]http://example.org/fourth.jpg[/img] Content C: Lorem Ipsum [img]http://example.org/fifth.jpg[/img] more lorem ipsum [img caption="Some other caption"]http://example.org[/img] EOT array = text.scan(regex) puts array </code></pre> <p>Which generates:</p> <pre> [img]http://example.org/first.jpg[/img] [img]http://example.org/second.jpg[/img] [img caption="Sample caption"]http://example.org/third.jpg[/img] [img]http://example.org/fourth.jpg[/img] [img]http://example.org/fifth.jpg[/img] [img caption="Some other caption"]http://example.org[/img] </pre> <p>If you want to ignore the tags and only grab the content, change the regexp to:</p> <pre><code>regex = /\[img(?:\s+caption=".+")?\](.+?)\[\/img\]/ </code></pre> <p>Running again with that change returns:</p> <pre><code>http://example.org/first.jpg http://example.org/second.jpg http://example.org/third.jpg http://example.org/fourth.jpg http://example.org/fifth.jpg http://example.org </code></pre> <p>(<a href="http://rubular.com/r/iBYvvzCoLW" rel="nofollow">Rubular proof</a>)</p> <p>If you need to look for different tags, you can generate an "OR" list easily:</p> <pre><code>Regexp.union(%w[foo img bar]) =&gt; /foo|img|bar/ </code></pre> <p>If you need to make sure that "magic" characters are escaped beforehand:</p> <pre><code>Regexp.union(%w[foo img bar].map{ |s| Regexp.escape(s) }) </code></pre>
 

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