简体   繁体   English

如何遍历多个过滤的正则表达式字符串

[英]How to loop over multiple filtered regex strings

I do not know how to better track down the question. 我不知道如何更好地追踪这个问题。 What I want to do is to look for every e-mail-address matching my regEx in different div.test , to create <a href="mailto:"> links. 我要做的是在不同的div.test与我的regEx匹配的每个电子邮件地址,以创建<a href="mailto:">链接。

This does work, but if there are multiple mail addresses inside one div, only the first one gets highlighted. 确实可以,但是如果一个div中有多个邮件地址,则仅第一个突出显示。 Whats the problem? 有什么问题?

var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;

$(".test").filter(function() {
    return $(this).html().match(regEx);
}).each(function() {
    $(this).html($(this).html().replace(regEx, "<a href=\"mailto:$1\">$1</a>"));
});

JSBIN 吉宾

You must add the global ( g ) flag to you regexp: 您必须将全局g )标志添加到您的regexp中:

var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/g

Otherwise your regex will halt after the first match. 否则,您的正则表达式将在第一场比赛后停止。

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

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