venus
venus2y ago

✅ – venus – 12-02 Oct 9

Looking for possible regex solution, since I still suck at it. The goal of the regex is to validate if string fits the format. First should be random length string, then comes colon (:) and the last is one of two options: desc or asc. someString:desc|asc Examples: firstName:desc, lastName:asc
7 Replies
ScriptyChris
ScriptyChris2y ago
@venus have you tried with /\w+:(desc|asc)/?
ScriptyChris
ScriptyChris2y ago
You can play with it on https://regex101.com/r/EcV8bn/1
regex101
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET.
venus
venus2y ago
Looks good to me, thanks. But when I add some string after desc or asc it will still return true value:
console.log({ matched: Boolean(/\w+:(desc|asc)/.test("firstName:desca")) });
console.log({ matched: Boolean(/\w+:(desc|asc)/.test("firstName:desca")) });
ScriptyChris
ScriptyChris2y ago
You can make regex to check for end of string after desc|asc via $
venus
venus2y ago
Beautiful! Thank you
reactibot
reactibot2y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1028638577975898113
reactibot
reactibot2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1028638577975898113 Question not getting answered? Maybe it's hard to answer, or maybe you asked at a slow time. Check out these resources for help asking a good question: https://stackoverflow.com/help/how-to-ask http://wp.me/p2oIwo-26