且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Regex删除Javascript中的HTML标记

更新时间:2022-12-28 11:50:23

试试这个,注意HTML的语法太复杂了,正规表达式在100%的时间都不正确:

Try this, noting that the grammar of HTML is too complex for regular expressions to be correct 100% of the time:

var regex = /(<([^>]+)>)/ig
,   body = "<p>test</p>"
,   result = body.replace(regex, "");

console.log(result);

如果你愿意使用像 jQuery ,你可以这样做:

If you're willing to use a library such as jQuery, you could simply do this:

console.log($('<p>test</p>').text());