Create a regular expression re to match opening-closing HTML tag pairs from the text, including their contents.
The array of groups should contain the whole tag, it’s name and it’s contents, see below:
re = /* your regexp */ result = "the **bold**".match(re) result[0] (whole tag) = '**bold**' result[1] (name) = 'b' result[2] (contents) = 'bold'
The solution should handle nested tags, like: the <b>bold <i>italic</i></b>. It should only match the outer tag in this case and ignore the nested one.
P.S. Use regexp <([a-z]\w*)> to match the opening tag.
So, the opening tag is <([a-z]\w*)>. Now we need to match the closing tag. We’ll use a backreference here.
The tag name is put into the group, so it can be referenced in closing tag:
str = "the **bold**" re = /<([a-z]\w*)>(.*?)<\/\1>/ alert( str.match(re) )
In the regexp above, the backreferene \1 refers to the first bracket group: ([a-z]\w*), which is the tag name.
Running same regexp on the text with nested tags:
re = /<([a-z]\w*)>(.*?)<\/\1>/ result = "the **bold *italic***".match(re) alert(result) // 0 => '**bold *italic***' // 1 => 'b' // 2 => 'bold *italic*'
It works.