2015-04-01 49 views
3

我想從html標記字符串名稱只提取。 我想獲得這樣的結果:正則表達式來轉義HTML標記

  • 農場1
  • STAGING
  • STAGING_SYSTEM_10

我可以使用什麼類型的正則表達式?

<div class='singleNode'><i class='fa fa-cogs'></i><span>Farm 1<span class='badge badge-primary'></span><span></div> 

<div class='singleNode'><i class='fa fa-cubes'></i><span>STAGING<span class='badge badge-primary'></span><span></div> 

<div class='singleNode'><i class='fa fa-desktop'></i><span>STAGING_SYSTEM_10<span class='badge badge-primary'></span><span></div> 
+3

使用HTML解析器而不是正則表達式。與HTML正則表達式的影響可以[不幸](http://stackoverflow.com/a/1732454/67392)。 – Richard 2015-04-01 08:29:49

回答

5

如果你必須使用正則表達式,這裏是示例代碼:

var re = /<div[^>]*?>(?:<(\S+)[^>]*?>[^<]*?<\/\1>)+<span[^]*?>([^<]*?)(?=<span)/g; 
 
var str = '<div class=\'singleNode\'><i class=\'fa fa-cogs\'></i><span>Farm 1<span class=\'badge badge-primary\'></span><span></div>\n\n <div class=\'singleNode\'><i class=\'fa fa-cubes\'></i><span>STAGING<span class=\'badge badge-primary\'></span><span></div>\n\n<div class=\'singleNode\'><i class=\'fa fa-desktop\'></i><span>STAGING_SYSTEM_10<span class=\'badge badge-primary\'></span><span></div>'; 
 
var m; 
 
    
 
while ((m = re.exec(str)) !== null) { 
 
    if (m.index === re.lastIndex) { 
 
     re.lastIndex++; 
 
    } 
 
    // View your result using the m-variable. 
 
    // m[2] is the 2nd capture group, the text inside the DIV element 
 
    alert(m[2]) 
 
}

如果你可以用DOM解析它,使用此代碼:

var input = document.getElementsByTagName("div"); 
 
for ($i = 0; $i < input.length; $i++) 
 
{ 
 
    alert(input[$i].textContent); 
 
}
<body> 
 
<div class='singleNode'><i class='fa fa-cogs'></i><span>Farm 1<span class='badge badge-primary'></span><span></div> 
 

 
<div class='singleNode'><i class='fa fa-cubes'></i><span>STAGING<span class='badge badge-primary'></span><span></div> 
 

 
<div class='singleNode'><i class='fa fa-desktop'></i><span>STAGING_SYSTEM_10<span class='badge badge-primary'></span><span></div> 
 
    </body>

+0

這個問題說HTML是在一個字符串中。在你這樣做之前,你需要把它變成一個DOM。 – Quentin 2015-04-01 08:55:48

+0

@Quentin:我也加了一個正則表達式解決方案。 – 2015-04-01 09:11:35

+0

謝謝! (λ= user3790694 2015-04-01 11:52:10