正则表达式恰好匹配5位数 [英] regular expression to match exactly 5 digits
问题描述
testing= testing.match(/(\d{5})/g);
我正在读一个完整的html变量。从变量中,想要获取具有正好5位数的模式的所有数字。无需关心此数字之前/之后是否有其他类型的单词。只是想确保抓取的是5位数字。
I'm reading a full html into variable. From the variable, want to grab out all numbers with the pattern of exactly 5 digits. No need to care of whether before/after this digit having other type of words. Just want to make sure whatever that is 5 digit numbers been grabbed out.
但是,当我申请它时,它不仅会拔出5位数字,而是还检索了超过5位数...
However, when I apply it, it not only pull out number with exactly 5 digit, number with more than 5 digits also retrieved...
我曾尝试将 ^
放在前面并且 $
落后,但它的结果是null。
I had tried putting ^
in front and $
behind, but it making result come out as null.
推荐答案
我正在阅读一个文本文件,并希望使用下面的正则表达式来提取正好5位数的数字,忽略字母。
I am reading a text file and want to use regex below to pull out numbers with exactly 5 digit, ignoring alphabets.
尝试这...
var str = 'f 34 545 323 12345 54321 123456',
matches = str.match(/\b\d{5}\b/g);
console.log(matches); // ["12345", "54321"]
jsFiddle 。
字边界 \ b
是你的朋友。
我的正则表达式会得到这样的数字 12345
,但不,如 a12345
。如果你需要后者,其他答案提供了很好的正则表达式。
My regex will get a number like this 12345
, but not like a12345
. The other answers provide great regexes if you require the latter.
这篇关于正则表达式恰好匹配5位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!