Home > other >  RegEx for getting the second number between hyphen
RegEx for getting the second number between hyphen

Time:11-26

I have a list of ids that go like this: dot-<number>-<number>-hash

My task is to isolate the second number between hyphen, like this:

"dot-0-12-0479b9f6fd75"  // 12

"dot-1-2-0479b9f6fd75"   // 2

"dot-2-234-0479b9f6fd75"  // 234

"dot-3-1012-0479b9f6fd75"  // 1012

I'm pretty sure there's a RegExp for that. Does anyone can help?

CodePudding user response:

If in your case all strings are the same, you do not need to use regex.

const text = "dot-0-12-0479b9f6fd75";

const getSecondNumber = (num) => num.split("-")[2];

console.log(getSecondNumber(text)); 
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

^\"dot\-[0-9] \-(?<secondnumber>[0-9] )\-[0-9a-f] \"$
  • Related