# Interpolate A String Into A Regex

You can build up strings with concatenation or the template literal syntax. If you're incorporating variables as part of this string building, that's called interpolation.

But what if you need to interpolate variables into a regular expression (regex)?

A regex literal looks like this:

```javascript
const myRegex = /^Some Customizable Text$/;
```

This works great for fixed strings, but won't cut it if you need to work *variable* strings.

What's needed to work with variables is the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor:

```javascript
const customizableString = "Some Customizable Text";
const myRegex = new RegExp(`^${customizableString}$`);
```

You can build up some string with string variables and regex syntax and `new RegExp()` will turn it into a regex.

[source](https://makandracards.com/makandra/15879-javascript-how-to-generate-a-regular-expression-from-a-string)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/javascript/interpolate-a-string-into-a-regex.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
