Validate sprintf format from input field with regex

后端 未结 3 1776
说谎
说谎 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条回答
  •  暖寄归人
    2021-01-21 00:41

    The UTF-8 modifier is not necessary unless you use UTF-8 in your pattern. And beside that the sprintf format is more complex, try the following

    /%(?:\d+\$)?[dfsu]/
    

    This would match both the %s and %1$s format.

    But if you want to check every occurrence of % and whether a valid sprintf() format is following, regular expressions would not be a good choice. A sequential parser would be better.

提交回复
热议问题