regexpreg-replace

Check user input is valid CSS width value


I'm allowing a user to type in a width value that I need to ensure is a valid CSS width value.

Using regex, how would I get two matches from the user input where $matches[0] = a number value AND $matches[1] = either px or %?

Thanks!


Solution

  • (\d*)(px|%)?
    

    First captured item will be the number, the second captured item will be px or %.