돌아가기

Find HTML tags

Create a regular expression to find all (opening and closing) HTML tags with their attributes.

An example of use:

let regexp = /your regexp/g;

let str = '<> <a href=/proxy/https/ko.javascript.info/task/&quot;/&quot;&gt; <input type="radio" checked> <b>';

alert( str.match(regexp) ); // '<a href=/proxy/https/ko.javascript.info/task/&quot;/&quot;&gt;', '<input type="radio" checked>', '<b>'

Here we assume that tag attributes may not contain < and > (inside squotes too), that simplifies things a bit.

The solution is <[^<>]+>.

let regexp = /<[^<>]+>/g;

let str = '<> <a href=/proxy/https/ko.javascript.info/task/&quot;/&quot;&gt; <input type="radio" checked> <b>';

alert( str.match(regexp) ); // '<a href=/proxy/https/ko.javascript.info/task/&quot;/&quot;&gt;', '<input type="radio" checked>', '<b>'