Baku Baku - 2 months ago 22
Javascript Question

Javascript regex error, text between two characters

(?<=\[)[^\]]*(?=\])


which is matching text:

[11/Sep/2016:21:58:55 +0000]


it works fine in sublime while testing, but when I do

str.match(/(?<=\[)[^\]]*(?=\])/)


Ive got error:
SyntaxError: Invalid regular expression

what Im doing wrong ?

Answer

You can use regex as :/[^\[\]]+/

const regex = /[^\[\]]+/;
const str = `[11/Sep/2016:21:58:55 +0000]`;
let m;

if ((m = regex.exec(str)) !== null) {
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}