I want to have accurate form field validation for NEAR protocol account names (one for .near
accounts and one for .testnet
accounts).
/\.testnet$/gm
is not quite a thorough enough regex for the testnet accounts.
The final 2 cases here should be marked as invalid (and there might be more cases that I don't know about):
example.testnet
sub.ex.testnet
something.testnet
shouldnotendwithperiod.testnet.
example.near
space should fail.testnet
touchingDotsShouldfail..testnet
See https://regex101.com/r/jZHtDA/1
I'm wondering if there is a well-tested regex for each type of account .near
and .testnet
) that I should be using in my validation.
Thanks.
CodePudding user response:
Something like this should do: /^(\w|(?<!\.)\.) (?<!\.)\.(testnet|near)$/gm
Breakdown
^ # start of line
(
\w # match alphanumeric characters
| # OR
(?<!\.)\. # dots can't be preceded by dots
)
(?<!\.) # "." should not precede:
\. # "."
(testnet|near) # match "testnet" or "near"
$ # end of line
Try the Regex out: https://regex101.com/r/vctRlo/1
CodePudding user response:
If you want to match word characters only, separated by a dot:
^\w (?:\.\w )*\.(?:testnet|near)$
Explanation
^
Start of string\w
Match 1 word characters(?:\.\w )*
Optionally repeat.
and 1 word characters\.
Match.
(?:testnet|near)
Match eithertestnet
ornear
$
End of string
A bit broader variant matching whitespace character excluding the dot:
^[^\s.] (?:\.[^\s.] )*\.(?:testnet|near)$