Skip to content
Advertisement

How to use preg_match to match numbers between hyphens?

I need to validate strings like this:

JavaScript

I need the penultimate number between hyphens, so in this case ’10’. These are other examples:

JavaScript

Can you help me?

Advertisement

Answer

this RegExp should do the trick using a positive lookahead: (change 4 to whatever max num of digits you want to allow)

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement