首页 > 解决方案 > 如何在三元运算符上告诉javascript“任何大于”的数字?

问题描述

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

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

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

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

标签: javascriptspringternary-operatorgatsby

解决方案


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

/\/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' }}
>

推荐阅读