[javascript] Regex match text between tags

I have this string:

My name is <b>Bob</b>, I'm <b>20</b> years old, I like <b>programming</b>.

I'd like to get the text between b tags to an array, that is:

['Bob', '20', 'programming']

I tried this /<b>(.*?)<\/b>/.exec(str) but it will only get the first text.

This question is related to javascript regex

The answer is


Try

str.match(/<b>(.*?)<\/b>/g);

var root = document.createElement("div");

root.innerHTML = "My name is <b>Bob</b>, I'm <b>20</b> years old, I like <b>programming</b>.";

var texts = [].map.call( root.querySelectorAll("b"), function(v){
    return v.textContent || v.innerText || "";
});

//["Bob", "20", "programming"]

Use match instead, and the g flag.

str.match(/<b>(.*?)<\/b>/g);