/    Sign up×
Community /Pin to ProfileBookmark

What does this regex do?

Can I please ask what does this regular expression is trying to do:
myDiv.replace(/<h([d])>([^<]+)</h([d])>/gi

I know it searches for header tags that are either h1 through to h6 but it seems very complicated, is there a simpler way to write this regular expression? Let me know if you need more detail.

Eddie

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 07.2009 — /&lt;(h[1-6])&gt;(.+?)&lt;/$1&gt;/gi
Is that any better?
Copy linkTweet thisAlerts:
@happyeddieauthorFeb 07.2009 — I'm affraid the code no longer works with that regex.

The original code is from: http://magnetiq.com/wpmain/wp-content/uploads/2008/10/toc.htm
Copy linkTweet thisAlerts:
@scragarFeb 07.2009 — sorry, it's a single leftslash, not a $, wrong type of regexp.
<i>
</i>/&lt;(h[1-6])&gt;(.+?)&lt;/1&gt;/gi
Copy linkTweet thisAlerts:
@happyeddieauthorFeb 07.2009 — hmmmm that also caused the script not to work.
Copy linkTweet thisAlerts:
@scragarFeb 07.2009 — huh, worked fine in my testing.
alert("&lt;h3&gt;Something&lt;/h3&gt;&lt;h1&gt;Something&lt;h4&gt;Else&lt;/h4&gt;&lt;/h1&gt;".match(/&lt;(h[1-6])&gt;(.+?)&lt;/1&gt;/gi));
Copy linkTweet thisAlerts:
@happyeddieauthorFeb 07.2009 — It doesn't seem to work inside the script for the code on this page:

http://magnetiq.com/wpmain/wp-content/uploads/2008/10/toc.htm
Copy linkTweet thisAlerts:
@scragarFeb 07.2009 — Meh, bad coding on that page, no need to see if the opening tag matches the ending tag with my code, forget about it.
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
window.onload = function () {
var toc = "";
var level = 0;

<i> </i>document.getElementById("contents").innerHTML =
<i> </i> document.getElementById("contents").innerHTML.replace(
<i> </i> /&lt;h([1-6])&gt;(.+?)&lt;/h1&gt;/gi,
<i> </i> function (str, openLevel, titleText) {
<i> </i> if (openLevel &gt; level)
<i> </i> toc += (new Array(openLevel - level + 1)).join("&lt;ul&gt;");
<i> </i> else if (openLevel &lt; level)
<i> </i> toc += (new Array(level - openLevel + 1)).join("&lt;/ul&gt;");

<i> </i> level = parseInt(openLevel);
<i> </i> var anchor = titleText.replace(/ /g, "_");
<i> </i> toc += "&lt;li&gt;&lt;a href="#" + anchor + ""&gt;" + titleText
<i> </i> + "&lt;/a&gt;&lt;/li&gt;";
<i> </i> return "&lt;h" + level + "&gt;&lt;a name="" + anchor + ""&gt;"
<i> </i> + titleText + "&lt;/a&gt;&lt;/h" + level + "&gt;";
<i> </i> }
<i> </i> );

<i> </i>if (level) {
<i> </i> toc += (new Array(level + 1)).join("&lt;/ul&gt;");
<i> </i>}

<i> </i>document.getElementById("toc").innerHTML += toc;
};
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="toc"&gt;
&lt;h3&gt;Table of Contents&lt;/h3&gt;
&lt;/div&gt;
&lt;hr/&gt;

<i> </i>&lt;div id="contents"&gt;
<i> </i> &lt;h1&gt;Fruits&lt;/h1&gt;
<i> </i> &lt;h2&gt;Red Fruits&lt;/h2&gt;
<i> </i> &lt;h3&gt;Apple&lt;/h3&gt;
<i> </i> &lt;h3&gt;Raspberry&lt;/h3&gt;
<i> </i> &lt;h2&gt;Orange Fruits&lt;/h2&gt;

<i> </i> &lt;h3&gt;Orange&lt;/h3&gt;
<i> </i> &lt;h3&gt;Tangerine&lt;/h3&gt;
<i> </i> &lt;h1&gt;Vegetables&lt;/h1&gt;
<i> </i> &lt;h2&gt;Vegetables Which Are Actually Fruits&lt;/h2&gt;
<i> </i> &lt;h3&gt;Tomato&lt;/h3&gt;
<i> </i> &lt;h3&gt;Eggplant&lt;/h3&gt;

<i> </i>&lt;/div&gt;
&lt;/body&gt;

Copy linkTweet thisAlerts:
@happyeddieauthorFeb 07.2009 — Thank you Sir ?
×

Success!

Help @happyeddie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...