2017-12-22 2 views
0

私はこの文字列を持って取得します。正規表現は、HTMLタグの間に言葉

<p><ins>Article </ins>Title</p> 

<p>Here&#39;s some sample text</p> 

私は言葉が配列にhtmlタグを無視取得したいのですが、つまり

['Article','Title','Here&#39;s','some','sample','text'] 

私は正規表現を作成しようとしたが、しそれは成功しません。 ありがとうございます。

+0

これまでに得た正規表現とは何ですか? – Eric

+0

var res = str.match(/ <[a-z]+>(。*?)<\/[a-z]+>/g).map(function(val){ return val; } – Beginner

+2

正規表現とHTMLは非常にうまく混在しません。https:// stackoverflowを参照してください。 .com/a/1732454/123681 – iblamefish

答えて

5

あなたは、このための正規表現を必要としない、あなたは、単にブラウザのAPIを使用することができますダミーdivでそれらを入れて、innerText

var str = `<p><ins>Article </ins>Title</p> 
 
<p>Here&#39;s some sample text</p>`; 
 

 
var div = document.createElement("div"); 
 
div.innerHTML = str; //assign str as innerHTML 
 
var text = div.innerText; //get text only 
 

 
var output = text.split(/\s+/); //split by one or more spaces including line feeds 
 
console.log(output);

3

を得る:

const html = "<p><ins>Article </ins>Title</p> <p>Here&#39;s some sample text</p>"; 
 
const div = document.createElement("div"); 
 
div.innerHTML = html; 
 

 
// This will extract the text (remove the HTML tags) 
 
const text = div.textContent || div.innerText || ""; 
 
console.log(text); 
 

 
// Then you can simply split the string 
 
const result = text.split(' '); 
 
console.log(result);

関連する問題