I have a list of keywords for example: key1, key2, key3, key4 ...
And I have a string, includes free strings and key:value pairs. I need to determine it valid or invalid. Here are the rules:
- If the string NOT contains keywords => VALID
- The
keyin pairkey:valueshould be belong to the keywords list. - The
valuein pairkey:valueshold not empty - There is no space before and after colon in pair
key:value - Do not accept any
key:valuethat is not in the keyword list
Example:
aaa bbb ccc=> VALIDaaa bbb key1:aaa ccc ddd=> VALIDaaa bbb key2:aaa key1:bbb=> VALIDaaa bbb key1 ccc=> INVALID (key1not follows the rulekey:value)aaa bbb key1:xxx key2:=> INVALID (key2not follows the rulekey:value)aaa bbb key1: xxx ccc=> INVALID (not accept space after colon)aaa bbb key1 :xxx ccc=> INVALID (not accept space before colon)aaa bbb aakey1=> VALID (aakey1is not the keyword)aaa bbb key1aa=> VALID (key1aais not the keyword)aaa bbb xxx:yyy zzz=> INVALID (Do not accept anykey:valuethat thekeyis not in the keyword list)
I have tried /^(?: ?(?:\b(?:key1|key2)(?:: *\S+|(*SKIP)(*FAIL))|\[^: \]+))*$/. It was good but didn't cover case aaa bbb key1aa. It got invalid instead of valid.
ALSO, how can I take the invalid keywords? Because I need to show error message to frontend.
Example: for aaa bbb key1 ccc, I need to take the error key here is key1. So that, I can show error message to frontend such as: Key1 is invalid format, please check it again....
Please help me take a look.