title: Javascript regular expression to replace iframe to clickable YouTube link
body: <p>I'm working importing a rss feed via javascript from a blog and using it to display elements to a mobile app, so DOM manipulation is not an option.</p>
  
  <p>I have a json parsed rss element that is a blog post that could contain one or more youtube video iframes, formated like this: <code>&lt;iframe src="//www.youtube.com/embed/0nvGQLnKg4Q" height="360" width="480" allowfullscreen="" frameborder="0"&gt;&lt;/iframe&gt;</code></p>
  
  <p>What I need is first get the <code>src</code> from iframe, store it into a variable to get the video code, then replace the <code>&lt;iframe...&gt;&lt;/iframe&gt;</code> to something like <code>&lt;a href="http://youtube.com/watch?v=YOUTUBEID"&gt;&lt;img src="http://img.youtube.com/vi/YOUTUBEID/1.jpg" width="100%" /&gt;&lt;/a&gt;</code></p>
  
  <p>The solutions I found were all PHP solutions and I have not been able to transpose them to Javascript. Reference: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/18653440/str-replace-regular-expression-to-remove-iframe">PHP Regular expression to remove iframe (which can be used to replace)</a></li>
  <li><a href="https://stackoverflow.com/questions/17017684/how-to-get-url-from-the-iframe-using-php">Get URL from iframe using PHP Regex</a></li>
  </ul>
  
  <p>I tried so far using javascript function</p>
  
  <pre><code>string.replace('.../g')
  </code></pre>
  
  <p>But no luck using exactly the same regexes from PHP. Besides, I need that to be made on all the iframes that could appear in my string, not only one or the first. What I was trying is basically like <a href="https://stackoverflow.com/questions/15103552/remove-iframe-with-javascript">this guy</a> was. But the answer to him (manipulating DOM) is not an option for me.</p>
  
  <p>Can anybody help me? Thanks in advance</p>