$('.change').click(function() {
    $('.link').each(function(e) {
        var color = this.className.match(/color-\w+/gi);
        alert(color);
    });
});
我基本上希望这样来提醒使用\w+而不是整个字符串找到的正则表达式。我怎样才能做到这一点?
此外,如何在color-不删除此实例之后删除正则表达式的情况下删除?
$('.change').click(function() {
    $('.link').each(function(e) {
        var color = this.className.match(/color-\w+/gi);
        alert(color);
    });
});
我基本上希望这样来提醒使用\w+而不是整个字符串找到的正则表达式。我怎样才能做到这一点?
此外,如何在color-不删除此实例之后删除正则表达式的情况下删除?
对颜色使用捕获组:
$('.change').click(function() {
    $('.link').each(function(e) {
        var pattern = /color-(\w+)/gi;
        var match = pattern.exec(this.className);
        var color = match[1];
        alert(color);
    });
});
您可以使用 javascript 正则表达式循环遍历将返回数组中的组的匹配项。您还可以使用replace带有组反向引用的函数来删除颜色-
$('.change').click(function() {
    $('.link').each(function(e) {
        // find regex matching groups
        var regex = /color-(\w+)/gi;
        var match;
        while (match = regex.exec(this.className)) {
            alert(match[1]);
        }
        // remove color-
        this.className = this.className.replace(/color-(\w+)/gi, "$1");
    });
});