Skip to content

Ch. 9 – dea+hl[]rd #403

Description

@mikegee0101

In this example:

let name = "dea+hl[]rd";
let text = "This dea+hl[]rd guy is super annoying.";
let escaped = name.replace(/[\\\[.+*?(){|^$]/g, "\\$&");
let regexp = new RegExp("\\b" + escaped + "\\b", "gi");
console.log(text.replace(regexp, "_$&_"));
// → This _dea+hl[]rd_ guy is super annoying.

Do you need that 3rd backslash at the beginning?

/[\\\[.+*?(){|^$]/g
https://scrn.es/WU5OBMAXhGTQHmYR6BuVi

vs.

/[\\[.+*?(){|^$]/g
https://scrn.es/4GiJj6ScfTCD22kmrLY1Dh

If i'm following, the first two represent an escaped backslash. Which means the 3rd one is escaping the open bracket. But do you need to do that here?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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