0

I need to check the following with 4 being the variable.

'1212'.match(/\d{4}/)

I know I can use '1212'.match(new RegExp(jamie)) to pass a variable into a regex, but then I don't know how to check for digits \d side of things.

1
  • 1
    RegExp() expects a string (or a regex pattern). So... Commented Feb 9, 2019 at 17:14

1 Answer 1

1

You can build a string with proper escaping and than using RegExp constructor you can build regex

let temp = 4
let reg = `\\d\{${temp}\}`
let regex = new RegExp(reg)

console.log('1212'.match(regex))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.