我有 guid 的正则表达式:
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
和正则表达式匹配一个数字:
/^[0-9]*$
但我需要匹配一个 guid 或一个数字。有没有办法匹配单个正则表达式。或者我应该分别检查 guid 和 number。
最佳答案
在正则表达式中使用 |
来匹配之前或之后的内容:
const re = /^([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})|[0-9]+$/i;
console.log('11111111-1111-1111-8111-111111111111'.match(re)[0]);
console.log('5555'.match(re)[0]);
关于javascript - 用于识别 guid 或数字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934082/