Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试匹配包含整数和空格的字符串。有人可以帮我吗?字符串长度为 3 个字符。在下面的示例中,我使用了字符“b”来表示空格字符。
有效的字符串是,
1bb
12b
123
b12
bb1
无效的字符串是,
bbb
使用这个正则表达式(?=.*?\d)(?=.*? ).+
(?=.*?\d)(?=.*? ).+
如果您知道字符串已经是 3 个字符,则可以使用
/^ {0,2}\d+ {0,2}$/
甚至更简单的
/^ *\d+ *$/
这是前面的可选空格,加上一个强制数字(或更多),最后是可选空格。
一个假设b2b也被认为是可以接受的。如果没有,那么
b2b
/^(\d+ *)|( *\d+)$/
可以使用。