-1

My question has previously been closed because it's apparently a duplicate of this one -- however, I don't want to replace anything, I want to see if specific strings match a general template.

I basically have dynamically generated strings containing ever-changing substrings made up of 11 random characters. E.g.:

"This is an example string: 5ju6hmama7l"
"This is an example string: 4nlc9jhm1ub"
"This is an example string: 5elk0k04nq9" 

etc.

I want to write code which verifies whether the dynamically generated string matches this general pattern, without having to specify the ever-changing substring. What I've tried so far:

const reg = /([a-z0-9]{10})\w/
const templateString = `This is an example string: ${reg}`
const actualString = "This is an example string: 5ju6hmama7l"

console.log(templateString.test(actualString))
console.log(templateString.match(actualString))
console.log(templateString.includes(actualString))

etc., but this hasn't worked. I'm sure there's an easy way to match the template string against the actual dynamically generated one, but I can't figure it out.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
rrr
  • 1
  • 1
  • I'm not sure the linked question hits the problem. Maybe you wanted a [capture group](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Capturing_group) – Christian Vincenzo Traina Aug 22 '23 at 13:37
  • @ChristianVincenzoTraina Thank you -- the linked question indeed doesn't hit my problem, since I don't want to replace anything. – rrr Aug 22 '23 at 14:12
  • "*I don't want to replace anything*" I fail to see how it's relevant. It shows how to create a dynamic regex from a string. Is that not what you're asking for? – VLAZ Aug 23 '23 at 06:31

0 Answers0