Http://www.cnblogs.com/liszt/archive/2011/08/16/2140007.html
function Removehtmltag (str) { str = str.replace (/<\/?[ ^>]*>/g, "); Remove HTML tag str = str.replace (/[|] *\n/g, ' \ n '); Remove line trailing blanks //str = str.replace (/\n[\s| |] *\r/g, ' \ n '); Remove extra empty line Str=str.replace (/ /ig, ");//Remove return str; }
Well, my request has been met.
Now let's explain briefly the three regular expressions used (it should be explained that because I was just in touch, perhaps my explanation is not correct, for reference only):
The first one:/<\/? [^>]*>/g
In JS, the expression is preceded by "/", the/g, meaning is the global schema, meaning that the matching pattern is applied to the entire string, rather than the first match after the stop matching.
<\/? [^>]*> This separate explanation, where the second character "\" is a transfer character that is used to transfer the trailing "/" character. Match 0 or 1 exactly the character that precedes it. Note: This meta-character is not supported by all software. So <\/? Matches the "</" format in the HTML tag or the "<" format.
Again, [^>]*>. [] The meaning is:
The meaning of ^ is: match the beginning of a line. For example, the regular expression ^when in is able to match the start of the string "When in the course of human events", but does not match "what is in the". This means matching text that begins with "when".
* means: Match 0 or more of the characters just before it. For example, regular expressions. * means being able to match any number of characters
So [^>]* meaning is a character that matches >. so [^>] The pattern can be matched like this:
- Div
- I need the text </div
- P
- I need the text </p
* and the previous [^>] together to match the following characters:
- div> I need the text </div
- p> I need the text </p
- BR/
Plus the > in the back can match the following characters:
- Div> the text I need </div>
- P> the text I need </p>
- br/>
This completes the matching of a pair of HTML tags. (A lot of words, always feel that the match is a bit verbose, but do not know where in the end to wordy)
The second one:/[|] *\n/g: I didn't read it either.
The third:/ /ig: is to directly find the character, the meaning of the following/ig is to make a case-insensitive lookup in global mode. G represents global, I means case insensitive.