wong2 wong2 - 1 year ago 107
Javascript Question

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
tags to an array, that is:

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

I tried this
but it will only get the first text.

Answer Source

Regular expression visualization

Add g (global) flag after:

             //^-----here it is 

However if you want to get all matched elements, then you need something like this:

var str = "<b>Bob</b>, I'm <b>20</b> years old, I like <b>programming</b>.";

var result = str.match(/<b>(.*?)<\/b>/g).map(function(val){
   return val.replace(/<\/?b>/g,'');
//result -> ["Bob", "20", "programming"] 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download