# Patterns and flags A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*. There are two syntaxes to create a regular expression object. The long syntax: ```js regexp = new RegExp("pattern", "flags"); ``` ...And the short one, using slashes `"/"`: ```js regexp = /pattern/; // no flags regexp = /pattern/gmi; // with flags g,m and i (to be covered soon) ``` Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings. ## Usage To search inside a string, we can use method [search](mdn:js/String/search). Here's an example: ```js run let str = "I love JavaScript!"; // will search here let regexp = /love/; alert( str.search(regexp) ); // 2 ``` The `str.search` method looks for the pattern `pattern:/love/` and returns the position inside the string. As we might guess, `pattern:/love/` is the simplest possible pattern. What it does is a simple substring search. The code above is the same as: ```js run let str = "I love JavaScript!"; // will search here let substr = 'love'; alert( str.search(substr) ); // 2 ``` So searching for `pattern:/love/` is the same as searching for `"love"`. But that's only for now. Soon we'll create more complex regular expressions with much more searching power. ```smart header="Colors" From here on the color scheme is: - regexp -- `pattern:red` - string (where we search) -- `subject:blue` - result -- `match:green` ``` ````smart header="When to use `new RegExp`?" Normally we use the short syntax `/.../`. But it does not support variable insertions `${...}`. On the other hand, `new RegExp` allows to construct a pattern dynamically from a string, so it's more flexible. Here's an example of a dynamically generated regexp: ```js run let tag = prompt("Which tag you want to search?", "h2"); let regexp = new RegExp(`<${tag}>`); // finds