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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4975644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:02:07  来源:igfitidea点击:

regular expression to match exactly 5 digits

javascriptregexmatchdigit

提问by i need help

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

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.

我正在将完整的 html 读入变量。从变量中,要抓取所有具有恰好 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...

但是,当我应用它时,它不仅提取出正好为 5 位的数字,而且还检索到了 5 位以上的数字...

I had tried putting ^in front and $behind, but it making result come out as null.

我曾尝试^在前后放置$,但它使结果显示为空。

回答by alex

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.

js小提琴

The word boundary \bis your friend here.

边界这个词\b是你的朋友。

Update

更新

My regex will get a number like this 12345, but notlike a12345. The other answers provide great regexes if you require the latter.

我的正则表达式会得到这样的数字12345,但不会a12345. 如果您需要后者,其他答案提供了很好的正则表达式。

回答by outis

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 引擎支持 regexp lookbehinds,你可以这样做:

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 variablerather than a literal regexp in the whileloop, otherwise you'll get an infinite loop.

请注意,对于 IE,似乎您需要使用存储在变量中RegExp而不是while循环中的文字 regexp ,否则您将获得无限循环。

回答by Mark Eirich

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>

回答by Luke

what is about this? \D(\d{5})\D

这是怎么回事? \D(\d{5})\D

This will do on:

这将用于:

f 23 23453 234 2344 2534 hallo33333 "50000"

f 23 23453 234 2344 2534 halo33333 “50000”

23453, 33333 50000

23453、33333 50000

回答by Muhammad Saqib

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 (?)

  • 12345abcd (?)

  • abcd12345 (?)

  • 0000aaaa2 (?)

  • a1234a5 (?)

  • 12345 (?)

  • <space>12345<space>12345 (??)

  • abc12345xyz (?)

  • 12345abcd (?)

  • abcd12345 (?)

  • 0000aaaa2 (?)

  • a1234a5 (?)

  • 12345(?)

  • <space>12345 <space>12345 (??)