R
Reactiflux

help-js

✅ – venus – 12-02 Oct 9

Vvenus10/9/2022
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
SScriptyChris10/9/2022
@venus have you tried with /\w+:(desc|asc)/? You can play with it on https://regex101.com/r/EcV8bn/1
Vvenus10/9/2022
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")) });
SScriptyChris10/9/2022
You can make regex to check for end of string after desc|asc via $
Vvenus10/9/2022
Beautiful! Thank you
UUUnknown User10/10/2022
3 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!