Search code examples
javascriptregexlookbehind

regex lookbehind in javascript


i im trying to match some words in text

working example (what i want) regex101:

regex = /(?<![a-z])word/g
text = word 1word !word aword

only the first three words will be matched which is what i want to achieve. but the look behind will not work in javascript :(

so now im trying this regex101:

regex = /(\b|\B)word/g
text = word 1word !word aword

but all words will match and they may not be preceded with an other letter, only with an integer or special characters. if i use only the smaller "\b" the 1word wont matchand if i only use the "\B" the !word will not match

Edit

The output should be ["word","word","word"]

and the 1 ! must not be included in the match also not in another group, this is because i want to use it with javascript .replace(regex,function(match){}) which should not loop over the 1 and !

The code i use it for

    for(var i = 0; i < elements.length; i++){
    text = elements[i].innerHTML;

    textnew = text.replace(regexp,function(match){
        matched = getCrosslink(match)[0];
        return "<a href='"+matched.url+"'>"+match+"</a>";
    });
    elements[i].innerHTML = textnew;
}

Solution

  • Capturing the leading character

    It's difficult to know exactly what you want without seeing more output examples, but what about looking for either starts with boundary or starts with a non-letter. Like this for example:

    (\bword|[^a-zA-Z]word)
    

    Output: ['word', '1word', '!word']

    Here is a working example


    Capturing only the "word"

    If you only want the "word" part to be captured you can use the following and fetch the 2nd capture group:

    (\b|[^a-zA-Z])(word)
    

    Output: ['word', 'word', 'word']

    Here is a working example


    With replace()

    You can use specific capture groups when defining the replace value, so this will work for you (where "new" is the word you want to use):

    var regex = /(\b|[^a-zA-Z])(word)/g;
    var text = "word 1word !word aword";
    
    text = text.replace(regex, "$1" + "new");
    

    output: "new 1new !new aword"

    Here is a working example

    If you are using a dedicated function in replace, try this:

    textnew = text.replace(regexp,function (allMatch, match1, match2){
        matched = getCrosslink(match2)[0];
        return "<a href='"+matched.url+"'>"+match2+"</a>";
    });
    

    Here is a working example