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

发布于 2024-10-16 23:49:34 字数 288 浏览 8 评论 0原文

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

我正在将完整的 html 读入变量中。想要从变量中取出所有具有恰好 5 位数字模式的数字。无需关心该数字前后是否有其他类型的单词。只是想确保 5 位数的数字都被抓出来了。

但是,当我应用它时,它不仅提取出正好 5 位数字的数字,还检索了超过 5 位数字的数字...

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

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.

However, when I apply it, it not only pull out number with exactly 5 digit, number with more than 5 digits also retrieved...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绝情姑娘 2024-10-23 23:49:34

我正在阅读一个文本文件,并希望使用下面的正则表达式来提取恰好 5 位数字的数字,忽略字母。

试试这个...

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 这样的数字。如果您需要后者,其他答案提供了很好的正则表达式。

I am reading a text file and want to use regex below to pull out numbers with exactly 5 digit, ignoring alphabets.

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.

The word boundary \b is your friend here.

Update

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

你是暖光i 2024-10-23 23:49:34

我的测试字符串如下:

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

如果我理解您的问题,您需要 ["12345", "54321", "15234", "52341"]

如果 JS 引擎支持正则表达式后视,您可以这样做:

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

因为目前不支持,您可以:

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

并从适当的结果中删除前导非数字,或者:

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

请注意,对于 IE,似乎您需要使用 存储在变量中的正则表达式而不是 while 中的文字正则表达式循环,否则你会得到一个无限循环。

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"].

If JS engines supported regexp lookbehinds, you could do:

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.

落叶缤纷 2024-10-23 23:49:34

这应该有效:

<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>

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>
我还不会笑 2024-10-23 23:49:34

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

这适用于:

f 23 23453 234 2344 2534 Hallo33333 "50000"

23453, 33333 50000

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

This will do on:

f 23 23453 234 2344 2534 hallo33333 "50000"

23453, 33333 50000

舞袖。长 2024-10-23 23:49:34

无需关心该数字前后是否有其他类型的单词

要只匹配字符串中任意位置的 5 位数字的模式,无论是否用空格分隔,请使用此正则表达式 (?< ;!\d)\d{5}(?!\d)

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] ...
        } 
    }

以下是一些快速结果。

  • abc12345xyz (✓)

  • 12345abcd (✓)

  • abcd12345 (✓)

  • 0000aaaa2 ( ✖)

  • a1234a5 (✖)

  • 12345 (✓)

  • <代码><空格> 12345<空格>12345 (✓✓)

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).

Sample JavaScript codes:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文