Skip to content

Useless regular-expression character escape #8

@Mobb-Fixer

Description

@Mobb-Fixer

Using a regular expression with string literal (new RegExp('[a-z]+')) instead of regular expression slash syntax (/[a-z]+/) should be done with caution.
Usually, to escape . (dot) symbol within a regular expression, a developer would use backslash. But when it's done within a string literal (new RegExp('\.')) JavaScript will first process the string itself and only then call RegExp constructor.
It means new RegExp('\.') is equivalent to /./, which unintendedly allows to match any character instead of the intended dot only.
This may result in some security issues. For example, consider the hostname matching regular expression new RegExp('www\.mysite\.com'). In this case an input of http://www-mysite.com/ will pass the validation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions