gpt4 book ai didi

javascript - 正则表达式 - 后跟字母时匹配一定数量的数字

转载 作者:行者123 更新时间:2023-12-02 17:45:13 24 4
gpt4 key购买 nike

我想匹配一个由 1-4 个数字组成的字符串,后跟一个特定的字母。

例如,如果我的字符串是

'The hardness for this rubber is 90a , the hardness for the other was 120a'

我想获取值“90a”和“120a”。

我试过^[0-9]{0,4}[a]$^([0-9]{0,4})[g]$

如果能与数字后面的空格匹配,即可获得奖励积分。例如/

'Rubber number 1 was 98 a , rubber number 2 was 89 a'

会匹配

'98 a'

'89 a'

最佳答案

没问题:

\b\d{1,4} *a\b

说明:

\b        # Start of number (or use (?<!\d) if you're not on JavaScript)
\d{1,4} # Match 1-4 digits
[ ]* # Match optional space(s)
a # Match the letter "a"
\b # Make sure no letter/digit follows

关于javascript - 正则表达式 - 后跟字母时匹配一定数量的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21828035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com