DmitrySoshnikov / babel-plugin-transform-modern-regexp

Babel plugin for modern RegExp features in JavaScript
MIT License
81 stars 8 forks source link

Unexpected token #8

Closed damianobarbati closed 6 years ago

damianobarbati commented 6 years ago

I'm trying to transpile the following (I just added this plugins to babel plugins array):

export const templateToRegexp = template => {
    let pattern = template;
    pattern = pattern.replace(/\/:(?<param>[\w\d]+?)(?=\/|$)/gi, '\\/(?<$<param>>[\\w\\d]+?)(?=\\/|$)');
    pattern = pattern.replace(/\/:(?<param>[\w\d]+?)\?/gi, '(\\/(?<=\\/)(?<$<param>>[\\w\\d]+?)(?=\\/|$))?');
    pattern = '^' + pattern + '$';

    const regexp = new RegExp(pattern, 'i');
    return regexp;
};

But I get:

    SyntaxError:

    /[.*+?^${}()|[\]\\]/g
                ^
    Unexpected token: "|" at 1:12.

Is there some syntax error in the regexps I'm trying to transpile? Note: everything works on Chrome.

DmitrySoshnikov commented 6 years ago

Hi, thanks for the report, and sorry for the delay. It turns out, this Babel plugin had very outdated version of the regexp-tree, which had the parser bug. I've just updated to the latest, and published v. 0.0.6, and the /[.*+?^${}()|[\]\\]/g should be parsable.

Notice though, that this plugin doesn't have runtime support, and you can't use (?<$<param>> in the replace string.

For the full support see the https://github.com/babel/babel/pull/7105.