# Variable inside Regex

**URL:** <https://forum.freecodecamp.org/t/variable-inside-regex/466090>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 22, 2021, 9:41pm UTC](https://forum.freecodecamp.org/t/variable-inside-regex/466090 "2021-06-22T21:41:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![astur1031](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@astur1031](https://forum.freecodecamp.org/u/astur1031)\
**Post date:** [June 22, 2021, 9:41pm UTC](https://forum.freecodecamp.org/t/variable-inside-regex/466090/1 "2021-06-22T21:41:18Z")

</div>

Hi everyone,

I am trying to put inside a regex a variable but I cannot get the right results I want.

I want to remove 40 characters from the string “stringToSend”, but if I add the variable “characterLength” inside the regex, it doesn’t work.

If I just add the integer 40 inside the regex, everything works fine.

How can I add that variable “characterLength” inside the regex rightly?

```auto
<script>
    var stringToSend = '9615f3837cf791fc4302a00ab4adb32dd4171b1e_00004.jpg';
    var characterLength = 40;
    var regexVar = new RegExp(/^\w{' + characterLength + '}\_/); // this regex doesn't work
    // var regexVar = new RegExp(/^\w{40}\_/); // this regex is working
    outputString = stringToSend.replace(regexVar, '');
    outputString = outputString.replace(/\.[^/.]+$/, '');

    console.log(outputString); // Wanted output: 00004
</script>
```

Thank you very much in advance,  
Astur

---

<div class="post-metadata">

**Author:** ![caryaharper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/caryaharper/32/209142_2.png) [@caryaharper](https://forum.freecodecamp.org/u/caryaharper)\
**Post date:** [June 22, 2021, 11:06pm UTC](https://forum.freecodecamp.org/t/variable-inside-regex/466090/2 "2021-06-22T23:06:00Z")

</div>

This would work:

```auto
var regexVar = new RegExp(`^\\w{${characterLength}}\\_`); 

```

In your example you hand the RegExp just an already formatted regular expression.

In mine I hand it a string, or to be more specific I hand it a template literal which the constructor can use to make a regular expression. Template literals allow for embedded expression inside `${}`.

> **[Template literals (Template strings) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)**
>
> Template literals are string literals allowing embedded
> expressions. You can use multi-line strings and string interpolation features with
> them.

---

<div class="post-metadata">

**Author:** ![astur1031](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@astur1031](https://forum.freecodecamp.org/u/astur1031)\
**Post date:** [June 24, 2021, 11:56am UTC](https://forum.freecodecamp.org/t/variable-inside-regex/466090/3 "2021-06-24T11:56:37Z")

</div>

Thank you very much. It worked fine!!

Best,

---

<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:** [December 23, 2021, 11:56pm UTC](https://forum.freecodecamp.org/t/variable-inside-regex/466090/4 "2021-12-23T23:56:42Z")

</div>

Closed on December 23, 2021, 11:56pm UTC

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