in my CLI users can specify what they want to use: A user command can look like this:
include=name1,name2,name3
category=name1,name2
category=name1
In another words, a command always consists of 3 parts:
include
or category
=
: is in every command ,
How can I test this to get always true but false on everything else.
I am really bad in regex but I tried something like this:
/\category|include=\w/.test(str);
to simply test, at least, the most easy alternative which would be category=name1
but without success.
Can someone help me with this?
You were on the right path. Here's a fixed regex:
/^(category|include)=\w+(,\w+)*$/.test(str);
Note:
+
after the \\w
so that you can have several characters (,\\w+)*
^
and $
) in order to check the whole string You can use this regex for your requorement:
/^(category|include)=(\w+(?:,\w+)*)$/
\\w+(?:,\\w+)*)
in the value part after =
will allow 1 or more of comma separated words.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.