# Using template literal inside of a regex

**URL:** <https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [April 2, 2021, 12:13am UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028 "2021-04-02T00:13:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tannerpace](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tannerpace/32/200731_2.png) [@tannerpace](https://forum.freecodecamp.org/u/tannerpace)\
**Post date:** [April 2, 2021, 12:13am UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/1 "2021-04-02T00:13:07Z")

</div>

**Tell us what’s happening:**  
May be I am going about this the wrong way, I am trying to use a regex to search for the target string at the end of the string. I attempted to use the target as a changeable variable. Only it is not working as intended.  
**Your code so far**

```js

function confirmEnding(str, target) {

let myRegex = /`${target}`$/
let result = myRegex.test(str)

return result;
}

confirmEnding("Bastian", "n");

```

```
  **Your browser information:**

```

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36`.

**Challenge:** Confirm the Ending

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [April 2, 2021, 12:18am UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/2 "2021-04-02T00:18:37Z")

</div>

Remember that regex is not a string. You can convert a string to regex, as explained [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).

---

<div class="post-metadata">

**Author:** ![MatchaCrisp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/matchacrisp/32/193162_2.png) [@MatchaCrisp](https://forum.freecodecamp.org/u/MatchaCrisp)\
**Post date:** [April 2, 2021, 12:39am UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/3 "2021-04-02T00:39:15Z")

</div>

Here is a discussion on stackoverflow about this issue [](https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression)[How do you use a variable in a regular expression?](https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression)

But by far the easiest way to solve this is by using something like `String.prototype.lastIndexOf();`

---

<div class="post-metadata">

**Author:** ![tannerpace](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tannerpace/32/200731_2.png) [@tannerpace](https://forum.freecodecamp.org/u/tannerpace)\
**Post date:** [April 2, 2021, 12:57pm UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/4 "2021-04-02T12:57:44Z")

</div>

a user can use a Template literal inside of a regular expression.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [April 2, 2021, 1:22pm UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/5 "2021-04-02T13:22:58Z")

</div>

Yes, you can. A template literal resolves to a string. You can convert strings to regex. You build your string (with a template literal if you want) and then convert it to regex with the method shown in the links above.

---

<div class="post-metadata">

**Author:** ![tannerpace](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tannerpace/32/200731_2.png) [@tannerpace](https://forum.freecodecamp.org/u/tannerpace)\
**Post date:** [April 2, 2021, 4:40pm UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/6 "2021-04-02T16:40:25Z")

</div>

Thanks for your help. Wanted to share my script for future refrence.  
Spoiler Below

```auto
  const a = target
  const regex2 = new RegExp(`${a}$`,)
  let result = regex2.test(str)
  
  
  
  console.log(regex2)

  return result;
}

confirmEnding("Bastian", "n");
```

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [April 2, 2021, 4:45pm UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/7 "2021-04-02T16:45:23Z")

</div>

Cool. Yeah, that’s what I was thinking. I’d only add that the comma probably isn’t needed there:

```auto
RegExp(`${a}`,)

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [October 2, 2021, 4:45am UTC](https://forum.freecodecamp.org/t/using-template-literal-inside-of-a-regex/454028/8 "2021-10-02T04:45:53Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
