onmyway onmyway - 4 years ago 165
Javascript Question

Extract Twitter handlers from string using regex in JavaScript

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 "

>
" that I am including in my output. How can I change my regex to be better, and drop the "
>
" from my output?

Here is an example of a text string value:

"<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>"


The desired output would be an array consisting of the following:

PlaymakersZA, Absa, DiepslootMTB


Here is an example of my regex:

var array = str.match(/>[a-z-_]+/ig)


Thank you!

Answer Source

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);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download