✅ – 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
S
ScriptyChris557d ago
@venus have you tried with /\w+:(desc|asc)/?
S
ScriptyChris557d 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.
V
venus557d 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")) });
S
ScriptyChris557d ago
You can make regex to check for end of string after desc|asc via $
V
venus557d ago
Beautiful! Thank you
UU
Unknown User557d ago