Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS RegExp是否删除所有HTML标记及其内容?_Javascript_Jquery_Regex - Fatal编程技术网

Javascript JS RegExp是否删除所有HTML标记及其内容?

Javascript JS RegExp是否删除所有HTML标记及其内容?,javascript,jquery,regex,Javascript,Jquery,Regex,例如,我需要从中获得: Before Bold <b>In Bold</b> After Bold 我试过: string.replace(/<.*>.*<\/.*>/,'') string.replace(/.*/,'') 但是它并没有像预期的那样工作。我不确定regex,但是使用jQuery,您可以轻松地删除子项并使用经典的一行程序返回HTML: var div = document.createElement("div"), r

例如,我需要从中获得:

Before Bold <b>In Bold</b> After Bold
我试过:

string.replace(/<.*>.*<\/.*>/,'')
string.replace(/.*/,'')

但是它并没有像预期的那样工作。

我不确定regex,但是使用jQuery,您可以轻松地删除子项并使用经典的一行程序返回HTML:

var div = document.createElement("div"),
    result = "",
    child;

div.innerHTML = str;
child = div.firstChild;

do {
    if (child.nodeType === 3) {
        result += child.nodeValue;
    }
} while (child = child.nextSibling);

console.log(result);
string = $('<div>').html(string).children().remove().end().html();
string=$('').html(string).children().remove().end().html();

我不太清楚regex,但是使用jQuery,您可以轻松地删除子项并使用经典的一行程序返回HTML:

string = $('<div>').html(string).children().remove().end().html();
string=$('').html(string).children().remove().end().html();
试试这个:

string.replace(/<([^>]+?)([^>]*?)>(.*?)<\/\1>/ig, "")
string.replace(/]+?)([^>]*?)>(.*?)>(.*?)/ig,“”)
这对我有用

看到它在工作

试试这个:

string.replace(/<([^>]+?)([^>]*?)>(.*?)<\/\1>/ig, "")
string.replace(/]+?)([^>]*?)>(.*?)>(.*?)/ig,“”)
这对我有用


查看它是否工作

您正在使用贪婪的regexp。结果在粗体“之前的
”和粗体“
之后的
”之间有两个空格。这是因为您只删除了html标记。这正是我所期望的。
/(]+)>)/ig
-。。。与正则表达式相比,我更喜欢VisioN的方法。我需要删除标记以及所有标记的innerHTML。adeneo显示的内容,仅移除标签。我不是在寻找最好的解决方案,而是在大多数情况下都有效的解决方案scenarios@Teemu是的,因为它没有删除
标记及其内容。您使用的是贪婪的regexp。结果在粗体“
之前的
”和粗体“
之后的
”之间有两个空格。这是因为您只删除了html标记。这正是我所期望的。
/(]+)>)/ig
-。。。与正则表达式相比,我更喜欢VisioN的方法。我需要删除标记以及所有标记的innerHTML。adeneo显示的内容,仅移除标签。我不是在寻找最好的解决方案,而是在大多数情况下都有效的解决方案scenarios@Teemu是的,因为它没有删除带有内容的
标记。您可能需要修剪双空格,也可能需要修剪双空格