2016-07-25 70 views
-1

我有一個包含HTML的字符串。我想用空格拆分這個字符串,但我不想要HTML。javascript正則表達式與空間和html分割

例如:

scrambled it to make a type specimen book. It <a href="http://php.net/manual/en/function.sprintf.php">http://php.net/manual/en/function.sprintf.php</a> some more text. 

分切時與空間的字符串時,它切斷錨標記一半。我想考慮<a>標籤或<br>標籤作爲一個單詞。

+0

您是否嘗試過'html'字符串中迭代節點? – guest271314

回答

1

您可以使用String.prototype.match()RegExp/\w+|<a\s.*\/a>|(<br>|<br\s\/>|<br\/>)/g

var str = `scrambled it to make 
 
<br> a type specimen book. 
 
<br/> It 
 
<a href="http://php.net/manual/en/function.sprintf.php"> 
 
http://php.net/manual/en/function.sprintf.php 
 
</a> 
 
<br /> some more text.`; 
 

 
console.log(str.match(/\w+|<a\s.*\/a>|(<br>|<br\s\/>|<br\/>)/g));