MALK
MALK

Reputation: 393

Multiple regEx matches

I am matching words that begin with certain prefixes.

This regex is working well for me but I am having trouble applying multiple terms in one match.

I thought it would work with

However the latter only returns matches from the second term.

Here is the full code:

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

Upvotes: 0

Views: 2897

Answers (1)

Sam Greenhalgh
Sam Greenhalgh

Reputation: 6136

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

Upvotes: 1

Related Questions