Validate sprintf format from input field with regex

后端 未结 3 1777
说谎
说谎 2021-01-21 00:15

I have an input field where both regular text and sprintf tags can be entered.

Example: some text here. %1$s done %2$d times

How do I validate the s

3条回答
  •  猫巷女王i
    2021-01-21 00:47

    I originally used Gumbo's regex to parse sprintf directives, but I immediately ran into a problem when trying to parse something like %1.2f. I ended up going back to PHP's sprintf manual and wrote the regex according to its rules. By far I'm not a regex expert, so I'm not sure if this is the cleanest way to write it:

    /%(?:\d+\$)?[+-]?(?:[ 0]|'.{1})?-?\d*(?:\.\d+)?[bcdeEufFgGosxX]/
    

提交回复
热议问题