Skip to content

Text replacements not working due to special RegExp characters not being escaped #19

Closed
@mathiasbynens

Description

@mathiasbynens

var re = new RegExp(r.from, "g");
content = content.replace(re, r.to);
does the following:

        var re = new RegExp(r.from, "g");

It should be something like:

        var re = new RegExp(escapeRegExpPattern(r.from), "g");

Otherwise, for example, any special RegExp characters such as . or ? in the input are not interpreted as the raw characters anymore. As a result the ${owner}/${repo}.svg?branch=${old} replacement currently does not work.

An example escapeRegExpPattern implementation can be found here: https://stackoverflow.com/a/9310752/96656

You could also use this String.prototype.replaceAll polyfill: https://github.com/es-shims/String.prototype.replaceAll (And then in the future, remove it once all Node.js versions you care about get native support.) That way, you don't have to bother converting the search strings into regular expressions just to do global replacement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions