多个regEx匹配

时间:2012-02-06 14:39:34

标签: jquery regex

我匹配以某些前缀开头的单词。

这个正则表达式对我来说效果很好但我在一场比赛中应用多个术语时遇到了麻烦。

  • 匹配前缀're':/re\S+/g;

我认为它适用于

  • 匹配前缀're'和'http':/(re|http)\S+/g;

然而,后者仅返回第二个词的匹配。

以下是完整代码:

    function replacePrefix(input){
        var re = /(#)\S+/g;         
        var specials = [];
        var match;
        while(match = re.exec(input)){
            $('#text').html(input.replace(specials[0], "<span class='special'>"+specials[0]+"</span>"));
        }
    }

1 个答案:

答案 0 :(得分:1)

function replacePrefix(input){
    var re = /\b(?:re|http)\S+/g;         
    var specials = [];
    var match;
    while(match = re.exec(input)){
        console.log(match);
    }
}
replacePrefix("This is a sentence with really great urls in it like http://coolurl.com";);

["really"]
["http://coolurl.com"]