简体   繁体   English

使用JavaScript中的正则表达式从字符串中提取Twitter处理程序

[英]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. 我想使用正则表达式从文本字符串中提取Twitter处理程序名称。 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! 谢谢!

You can use match groups in your regex to indicate the part you wish to extract. 您可以在正则表达式中使用匹配组来指示要提取的部分。

I set up this JSFiddle to demonstrate. 我设置了这个JSFiddle进行演示。

Basically, you surround the part of the regex that you want to extract in parenthesis: />([az-_]+)/ig , save it as an object, and execute .exec() as long as there are still values. 基本上,您将要提取的正则表达式部分用括号括起来: />([az-_]+)/ig ,将其保存为对象,并在仍有值的情况下执行.exec()。 Using index 1 from the resulting array, you can find the first match group's result. 使用结果数组中的索引1,可以找到第一个匹配组的结果。 Index 0 is the whole regex, and next indices would be subsequent match groups, if available. 索引0是整个正则表达式,下一个索引将是后续的匹配组(如果有)。

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);
}

You could just strip all the HTML 您可以剥离所有HTML

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>";
$handlers = str.replace(/<[^>]*>|\s/g,'').split(",");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM