Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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正则表达式html标记_Javascript_Jquery - Fatal编程技术网

Javascript正则表达式html标记

Javascript正则表达式html标记,javascript,jquery,Javascript,Jquery,如果我在Javascript或jQuery中使用正则表达式编写text-example,如何获得这样的文本 例如: 如果我在 类型 只是一个没有标签的字体50 谢谢假设以下html <div>Random content can go <font size="50">here</font> with forn elements <font size="50">mixed into the text</font>, in random p

如果我在JavascriptjQuery中使用正则表达式编写
text-example
,如何获得这样的文本

例如: 如果我在

类型

只是一个没有标签的字体50


谢谢

假设以下html

<div>Random content can go <font size="50">here</font> with forn elements
<font size="50">mixed into the text</font>, in random positions.</div>
对于结果2

heremixed into the text

这里有一个指向JSFIDLE的链接,其中有一个工作示例

您不需要使用正则表达式,这是异端,通常是个坏主意

您可以改用
.text()

alert($('<font size="50"> text example </font>').text());

你要找的东西叫做。 早些时候,如果要检索标记中的文本,则有“innerText”。 要讨论textContent与innerText的利弊,请阅读上述文章。 如果查看源代码,jQuery的(sizzle.getText)将使用textContent:

if ( typeof elem.textContent === "string" ) {
        return elem.textContent;

我建议你解释得更详细一点,你的问题没有多大意义,可能会被否决。为什么你需要使用正则表达式呢?JavaScript(和jQuery)具有从DOM元素提取内容的功能。在给出的示例中,
$('font').text()
将提取您想要的内容。对于更复杂的标记,请相应地更改选择器。您忘记了text(),这将只返回html对象(字体)。是的,我当然忘记了。text(),因为这将返回所有选定的标记,因此。text()可能毫无意义。您返回的对象包含更多关于字体标记的格式,他需要文本值。对象包含字体属性(例如大小)等信息。所以text()并不是毫无意义的。这里有一个详细的答案@BojanKovacevic。我相信它满足了您使用.text()的要求,并解释了为什么立即使用.text()可能毫无意义。现在,它的解释更好了。无论如何,您可以得出结论,他将拥有更多具有相同大小属性的字体元素,在他的示例中,情况并非如此。在他的例子中,text()就足够了。但是,如果您想向他展示在获取多个元素的情况下如何继续,那么您需要像刚才那样解释它。不仅仅是放置只包含html对象的jquery选择器。更新的答案现在值得+所以我会给它.console.log(getText)$('font').text();log(getText);我试着这样做,但在这两种情况下,我得到了相同的输出:文本你做错了什么。这一定行得通。检查一下
//first get all elements with the appropriate selector
var $fontElements = $("font[size='50']");

//then access them one by one using .each(). $(this).text() gets the actual text
$fontElements.each(function (index) {
    var line = "element " + index + "=>" + $(this).text();
    $("#result1").append(line);
    $("#result1").append($("<br>"));
});

//using .text() right away will mix all the elements into one continous sting.
$("#result2").text($fontElements.text());
element 0=>here
element 1=>mixed into the text
heremixed into the text
alert($('<font size="50"> text example </font>').text());
alert($('font[size=50]').text());
if ( typeof elem.textContent === "string" ) {
        return elem.textContent;