如何在三元运算符上告诉javascript“任何大于”的数字?

问题描述 投票:0回答:1

我需要建立一个具有条件的三元运算符:每当URL为/ index / plus时,任何大于“1”的数字都为X.

我试过这个(带有“to”的字符串:

<Spring
    from={{ height: location.pathname === '/' ? '0vh' : '0vh' }}
    to={{ height: (location.pathname === '/' || location.pathname === '/index/' + (>= 2) ) ? '36vh' : '0vh' }}
>

不幸的是,它不起作用。这是一个分页问题(我不知道将创建多少页面)。

javascript spring ternary-operator gatsby
1个回答
1
投票

这与条件运算符无关。它与匹配字符串有关。如果你想将location.pathname/index/n匹配,n必须大于1,你可能需要一个正则表达式:

/\/index\/(?:[2-9]|\d{2,})/.test(location.pathname)

(?:...)是一个非捕获组。 [2-9]|\d{2,}是一个交替,匹配[2-9]\d{2,}[2-9]匹配2到9之间的任何数字,包括2和9。 \d{2,}匹配两个或更多数字。

在上下文中:

<Spring
    from={{ height: location.pathname === '/' ? '0vh' : '0vh' }}
    to={{ height: (location.pathname === '/' || /\/index\/(?:[2-9]|\d{2,})/.test(location.pathname) ) ? '36vh' : '0vh' }}
>
© www.soinside.com 2019 - 2024. All rights reserved.