In the course of the interview encountered a topic such as:
For example, there is an HTML string:
var string= ' <div class ' test ' >test a code</div><em>i am a girl.</em> '; Requires writing a regular expression that matches the HTML tag, and replaces all tags.
The HTML tag has
In fact, the regular expression matching HTML tags is very short, as follows:
<[^>]+>
That's it, it's gone? Yes. Are you surprised?
This regular expression is also collected in the network, but I can understand, first of all, the HTML tag is definitely starting with the < symbol, the end of the > symbol (whether it is not self-closing), just a little tangled in the middle, there may be characters, numbers, quotes, newline characters--but, but there is no; Because of it, the HTML tag ends, so the middle of the regular expression is [^>]+, which means " any more than 1 characters apart from the > Symbol ".
Examples are as follows:
var string= '<class ' test '>test a code</div> <em>I am a girl. </ em >';
Regular:
var reg=/<[^>]+>/gim;
Output:
Test a Codei am a girl.
JavaScript Regular expression: matches all HTML tags