<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>piouPiouM&#039;s dev&#187; Archives pour le tag HTML – piouPiouM&#039;s dev</title>
	<atom:link href="http://pioupioum.fr/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://pioupioum.fr</link>
	<description>Bloc-note d&#039;un développeur web</description>
	<lastBuildDate>Fri, 23 Dec 2011 23:41:36 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Capturer les balises HTML et leur contenu avec une RegExp sous JavaScript</title>
		<link>http://pioupioum.fr/snippets/javascript-regex-capture-html-balise-contenu.html</link>
		<comments>http://pioupioum.fr/snippets/javascript-regex-capture-html-balise-contenu.html#comments</comments>
		<pubDate>Wed, 12 Aug 2009 07:00:06 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=198</guid>
		<description><![CDATA[Voici une expression régulière pour capturer les tags HTML doubles1 sous JavaScript.


/&#60;&#40;\w+&#41;&#40;&#40;?:\s+\w+&#40;?:\s*=\s*&#40;?:&#34;[\s\S]*?&#34;&#124;'[\s\S]*?'&#124;&#91;^'&#34;&#62;\s]+))?)+\s*&#124;\s*)&#62;([\s\S]*?)&#60;\/\1&#62;/gi


Considérons la phrase&#160;:

Testons une &#60;tag class="foobar" title="un &#62; dans l'attribut"&#62;regexp en Javascript&#60;/tag&#62; complexe.


La regex va capturer les éléments suivants&#160;:


RegExp.$0 = "&#60;tag class="foobar"&#62;regex en Javascript&#60;/tag&#62;". Le texte qui satisfait le masque complet.
RegExp.$1 = "tag". Le nom de la balise HTML.
RegExp.$2 = " class="foobar" title="un &#62; [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Voici une <a href="http://fr.wikipedia.org/wiki/Expression_rationnelle" title="Expression rationnelle - Wikipédia"><strong>expression régulière</strong></a> pour capturer les tags HTML doubles<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> sous <a href="http://fr.wikipedia.org/wiki/JavaScript" title="JavaScript - Wikipédia">JavaScript</a>.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span><span style="color: #009900;">&#40;</span>\w<span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:</span>\s<span style="color: #339933;">+</span>\w<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:</span>\s<span style="color: #339933;">*=</span>\s<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:</span><span style="color: #3366CC;">&quot;[<span style="color: #000099; font-weight: bold;">\s</span><span style="color: #000099; font-weight: bold;">\S</span>]*?&quot;</span><span style="color: #339933;">|</span><span style="color: #3366CC;">'[<span style="color: #000099; font-weight: bold;">\s</span><span style="color: #000099; font-weight: bold;">\S</span>]*?'</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">^</span><span style="color: #3366CC;">'&quot;&gt;<span style="color: #000099; font-weight: bold;">\s</span>]+))?)+<span style="color: #000099; font-weight: bold;">\s</span>*|<span style="color: #000099; font-weight: bold;">\s</span>*)&gt;([<span style="color: #000099; font-weight: bold;">\s</span><span style="color: #000099; font-weight: bold;">\S</span>]*?)&lt;<span style="color: #000099; font-weight: bold;">\/</span><span style="color: #000099; font-weight: bold;">\1</span>&gt;/gi</span></pre></div></div>


<p>Considérons la phrase&#160;:</p>

<pre><code>Testons une &lt;tag class="foobar" title="un &gt; dans l'attribut"&gt;regexp en Javascript&lt;/tag&gt; complexe.
</code></pre>

<p>La regex va capturer les éléments suivants&#160;:</p>

<ul>
<li><code>RegExp.$0 = "&lt;tag class="foobar"&gt;regex en Javascript&lt;/tag&gt;"</code>. Le texte qui satisfait le masque complet.</li>
<li><code>RegExp.$1 = "tag"</code>. Le nom de la balise HTML.</li>
<li><code>RegExp.$2 = " class="foobar" title="un &gt; dans l'attribut""</code>. Les attributs.</li>
<li><code>RegExp.$3 = "regex en Javascript"</code>. Le contenu englobé par les balises ouvrantes et fermantes.</li>
</ul>

<p>Il sera possible de traiter par la suite les attributs de l&#8217;élément.</p>

<p>Merci à Phil Haack pour cette <a href="http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx" title="Using a Regular Expression to Match HTML">expression régulière</a> adaptée ici pour JavaScript<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup>.</p>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/outils-astuces/facebook-application-add-page-tab-bookmarklet.html' title='Bookmarklet : ajout rapide d&#8217;application onglet à vos pages fan Facebook'>Bookmarklet&#160;: ajout rapide d&#8217;application onglet à vos pages fan Facebook</a></li><li><a href='http://pioupioum.fr/developpement/javascript-array-intersection.html' title='JavaScript : optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles'>JavaScript&#160;: optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles</a></li><li><a href='http://pioupioum.fr/developpement/jslint-console-rhino.html' title='Une console JSLint pour aider vos validations JavaScript'>Une console JSLint pour aider vos validations JavaScript</a></li><li><a href='http://pioupioum.fr/developpement/optimiser-rapidite-chargement-adsense-jquery.html' title='Optimiser le chargement des AdSense'>Optimiser le chargement des AdSense</a></li><li><a href='http://pioupioum.fr/developpement/jquery-color-plugin-animation-rgba-support.html' title='Du RGBA dans vos animations avec le plugin jQuery Color'>Du RGBA dans vos animations avec le plugin jQuery Color</a></li></ul>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>elle ne tient donc pas compte des balises comme <code>&lt;br/&gt;</code>, <code>&lt;img/&gt;</code> ou <code>&lt;hr/&gt;</code>.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:2">
<p>notamment pour la capture des <a href="http://blog.jaysalvat.com/articles/capturer-un-retour-a-la-ligne-en-javascript.php" title="Snippet Regexp : Capturer un retour à la ligne en JavaScript  | Jay Salvat, le Blog">retours à la ligne</a> et des attributs.&#160;<a href="#fnref:2" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/snippets/javascript-regex-capture-html-balise-contenu.html/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

