JS: Create Regex Object

By Xah Lee. Date: . Last updated: .

Regex Literal Expression

/regex/flags

This is convenient.

If you have slash character in regex pattern, you need to add backslash in front to escape it.

Regex constructor

RegExp(str, flagsStr)

This is more general, and can be used to construct regex from string at runtime.

JavaScript. Regular Expression