简体   繁体   English

正则表达式精确匹配5位数字

[英]regular expression to match exactly 5 digits

testing= testing.match(/(\d{5})/g);

I'm reading a full html into variable. 我正在阅读完整的html变量。 From the variable, want to grab out all numbers with the pattern of exactly 5 digits. 从变量中,要截取所有具有精确5位数字的数字。 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位数字。

However, when I apply it, it not only pull out number with exactly 5 digit, number with more than 5 digits also retrieved... 但是,当我应用它时,它不仅提取出准确的5位数字,而且还检索了超过5位的数字...

I had tried putting ^ in front and $ behind, but it making result come out as null. 我曾尝试将^放在前面,将$放在后面,但是结果却为null。

I am reading a text file and want to use regex below to pull out numbers with exactly 5 digit, ignoring alphabets. 我正在阅读一个文本文件,并希望使用下面的正则表达式来提取5位数字,而忽略字母。

Try this... 尝试这个...

var str = 'f 34 545 323 12345 54321 123456',
    matches = str.match(/\b\d{5}\b/g);

console.log(matches); // ["12345", "54321"]

jsFiddle . jsFiddle

The word boundary \\b is your friend here. 边界\\b是您的朋友。

Update 更新资料

My regex will get a number like this 12345 , but not like a12345 . 我的正则表达式将得到一个像这样的数字12345 ,而不是a12345这样的a12345 The other answers provide great regexes if you require the latter. 如果您需要后者,其他答案也提供了很好的正则表达式。

My test string for the following: 我的测试字符串如下:

testing='12345,abc,123,54321,ab15234,123456,52341';

If I understand your question, you'd want ["12345", "54321", "15234", "52341"] . 如果我理解您的问题,则需要["12345", "54321", "15234", "52341"]

If JS engines supported regexp lookbehinds, you could do: 如果JS引擎支持正则表达式后退,则可以执行以下操作:

testing.match(/(?<!\d)\d{5}(?!\d)/g)

Since it doesn't currently, you could: 由于目前还没有,您可以:

testing.match(/(?:^|\D)(\d{5})(?!\d)/g)

and remove the leading non-digit from appropriate results, or: 并从适当的结果中删除前导的非数字,或:

pentadigit=/(?:^|\D)(\d{5})(?!\d)/g;
result = [];
while (( match = pentadigit.exec(testing) )) {
    result.push(match[1]);
}

Note that for IE, it seems you need to use a RegExp stored in a variable rather than a literal regexp in the while loop, otherwise you'll get an infinite loop. 请注意,对于IE,似乎您需要使用存储在变量中RegExp而不是while循环中的文字regexp,否则您将获得无限循环。

This should work: 这应该工作:

<script type="text/javascript">
var testing='this is d23553 test 32533\n31203 not 333';
var r = new RegExp(/(?:^|[^\d])(\d{5})(?:$|[^\d])/mg);
var matches = [];
while ((match = r.exec(testing))) matches.push(match[1]);
alert('Found: '+matches.join(', '));
</script>

what is about this? 这是什么 \\D(\\d{5})\\D

This will do on: 这将在:

f 23 23453 234 2344 2534 hallo33333 "50000" f 23 23453 234 2344 2534 hallo33333“ 50000”

23453, 33333 50000 23453,33333 50000

No need to care of whether before/after this digit having other type of words 无需担心此数字之前/之后是否还有其他类型的单词

To just match the pattern of 5 digits number anywhere in the string, no matter it is separated by space or not, use this regular expression (?<!\\d)\\d{5}(?!\\d) . 要仅匹配字符串中任意位置的5位数字的模式,无论是否用空格将其分隔,请使用此正则表达式(?<!\\d)\\d{5}(?!\\d)

Sample JavaScript codes: 示例JavaScript代码:

var regexp = new RegExp(/(?<!\d)\d{5}(?!\d)/g); 
    var matches = yourstring.match(regexp);
    if (matches && matches.length > 0) {
        for (var i = 0, len = matches.length; i < len; i++) {
            // ... ydo something with matches[i] ...
        } 
    }

Here's some quick results. 这是一些快速的结果。

  • abc12345xyz (✓) abc12345xyz(✓)

  • 12345abcd (✓) 12345abcd(✓)

  • abcd12345 (✓) abcd12345(✓)

  • 0000aaaa2 (✖) 0000aaaa2(✖)

  • a1234a5 (✖) a1234a5(✖)

  • 12345 (✓) 12345(✓)

  • <space> 12345 <space> 12345 (✓✓) <space> 12345 <space> 12345(✓✓)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM