开发者

How to test for many conditions?

开发者 https://www.devze.com 2023-03-25 03:37 出处:网络
How do I force section to be either asd or alg or 开发者_如何学运维sys or data or sim or se ?

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)$/.

0

精彩评论

暂无评论...
验证码 换一张
取 消