javascript - 如何在三元运算符上告诉javascript“任何大于”的数字?
问题描述
我需要建立一个带有条件的三元运算符:每当 URL 是 /index/ 加上任何大于“1”的数字时,X.
我试过这个(带有“to”属性的字符串:
<Spring
from={{ height: location.pathname === '/' ? '0vh' : '0vh' }}
to={{ height: (location.pathname === '/' || location.pathname === '/index/' + (>= 2) ) ? '36vh' : '0vh' }}
>
不幸的是,它不起作用。这是一个分页问题(我不知道会创建多少页)。
解决方案
这与条件运算符无关。它与匹配字符串有关。如果你想匹配wherelocation.pathname
必须大于 1,你可能需要一个正则表达式:/index/n
n
/\/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' }}
>
推荐阅读
- python - 如何更新列表中类的实例
- bash - 使用 sed 从 csv 文件中删除行并保存文件而不删除行
- vue.js - 不要在突变处理程序之外改变 Vuex 存储状态(错误)
- angular - 使用工具栏强制 Angular Material sidenav 容器填充高度
- kubernetes - 如何列出集群中的所有命名空间?
- watson-assistant - 如何访问 Watson Assistant 的全局上下文
- python - 如何使用 Python 在 Telegram bot API 中使键盘按钮消息文本与其标题不同
- git - git 提交分阶段更改的子集(按文件名)
- google-sheets - 从多个工作表中提取列左侧的所有信息
- sql - 如何创建存储过程?