I have a regular expression which removes html code from a String :
var html = "<p>Dear sms,</p><p>This is a test notification for push message from center II.</p>";
text = html.replace(/(<([^>]+)>)/ig, "")
alert(text)
This is the expression working on jsfiddle : http://jsfiddle.net/VgHr3/53/
The regular expression itself is /(<([^>]+)>)/ig
. I don't fully understand how this expression works. Can provide an explanation ? I can find what each character by itself behaves by reading a cheatsheet : http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
But what is the significance of "/ig" ?
Those are global flags. Your cheat sheet actually lists them on the right side:
Regular Expressions Pattern Modifiers
g Global match
i Case-insensitive
m Multiple lines
s Treat string as single line
x Allow comments and white space in pattern
e Evaluate replacement
U Ungreedy pattern
Note that not all of these flags are supported by the JavaScript regular expression engine. For an authoritative list, see this MDN article .
So the "g" flag makes it global, so it replaces this pattern wherever it is found, instead of just the first instance (which is the default behavior of the replace
method).
The "i" flag makes it case-insensitive, so a pattern like [az]+
will match "foo"
and "FOO"
. However, because your pattern only involves <
and >
characters, this flag is useless.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.