How do I force section
to be either asd
or alg
or 开发者_如何学运维sys
or data
or sim
or se
?
If I use regex like below, then datadfghdg
would be allowed.
if (section == '') {
alert('Section is required');
} else if (!(/asd|alg|sys|data|img|se/.test(section))) {
alert('Section must be one of: asd alg sys data img se');
} else {
is_okay = 1;
}
You can force the strings to be at the beginning and and of the regex by using ^
and ^$` respectively:
if( !(/^(asd|alg|sys|data|img|se)$/.test(section)) )
...
Alternatively you can use in
to achieve this as well:
if( section in { 'asd':'','alg':'', ... } )
...
You need to add ^ to the beginning and $ to the end of the regex.
/^(asd|alg|sys|data|img|se)$/
Try with /^(asd|alg|sys|data|img|se)$/.
Use the regex /^(asd|alg|sys|data|img|se)$/
.
精彩评论