I Would like to extract the Twitter handler names from a text string, using a regex. I believe I am almost there, except for the "
>
>
"<a href=\"https://twitter.com/PlaymakersZA\" target=\"_blank\">PlaymakersZA</a>, <a href=\"https://twitter.com/Absa\" target=\"_blank\">Absa</a>, <a href=\"https://twitter.com/DiepslootMTB\" target=\"_blank\">DiepslootMTB</a>"
PlaymakersZA, Absa, DiepslootMTB
var array = str.match(/>[a-z-_]+/ig)
You can use match groups in your regex to indicate the part you wish to extract.
I set up this JSFiddle to demonstrate.
Basically, you surround the part of the regex that you want to extract in parenthesis: />([a-z-_]+)/ig
, save it as an object, and execute .exec() as long as there are still values. Using index 1 from the resulting array, you can find the first match group's result. Index 0 is the whole regex, and next indices would be subsequent match groups, if available.
var str = "<a href=\"https://twitter.com/PlaymakersZA\" target=\"_blank\">PlaymakersZA</a>, <a href=\"https://twitter.com/Absa\" target=\"_blank\">Absa</a>, <a href=\"https://twitter.com/DiepslootMTB\" target=\"_blank\">DiepslootMTB</a>";
var regex = />([a-z-_]+)/ig
var array = regex.exec(str);
while (array != null) {
alert(array[1]);
array = regex.exec(str);
}