使用正则表达式仅检索7位数的字符串

时间:2021-05-07 19:29:43

In short, the page contains about 10 divs, each containing letters and numbers and I'm trying to return all occurrences of exactly 7 digits, but can have other, non-digit characters before and after.

简而言之,该页面包含大约10个div,每个div包含字母和数字,我试图返回所有出现的7个数字,但前后可以有其他非数字字符。

eg.

例如。

"q1234567" should return 1234567

“q1234567”应返回1234567

"q1234567q" should return 1234567

“q1234567q”应返回1234567

"q1234567q1234567q12345678q" should return 1234567 and 1234567

“q1234567q1234567q12345678q”应返回1234567和1234567

"12345678" should NOT be returned

不应退回“12345678”

To be more specific, an example of an entire string:

更具体地说,整个字符串的示例:

q1234567q
q1234567q
q12345678q
q1234567q123456789q123456q1324567q1234567
1234567
1
12
123
1234
12345
q12345q
q1234
12345q
123

I tried doing this via regex and got as far as

我尝试通过正则表达式做到这一点,并得到了

/\d{7}(?=\D|$)/g

but JavaScript doesn't play well with the lookbehind.. How can I get around this without involving a whole new library?

但是JavaScript并没有很好地适应这种情况。如何在不涉及全新库的情况下解决这个问题?

5 个解决方案

#1


2  

You could maybe use something like this?

你可以使用这样的东西吗?

var regex = /(?:^|\D)(\d{7})(?!\d)/g;
var s = "q1234567q123456789q123456q1324567q1234567";
var match, matches=[];

while ( (match=regex.exec(s)) !== null ) {
    matches.push(match[1]);
}

alert(matches);

jsfiddle demo

jsfiddle演示

#2


4  

This regex should work:

这个正则表达式应该工作:

/^\D*\d{7}\D*$/

Online Demo: http://regex101.com/r/nE5eI6

UPDATE: As per edited question and comments below you can use this regex:

更新:根据下面编辑的问题和评论,您可以使用此正则表达式:

(?:^|\D)(\d{7})(?=\D|$)

And use matched group #1 for your output.

并使用匹配的组#1作为输出。

Demo: http://regex101.com/r/wL4oW1

#3


0  

What about: (?<=\D)[0-9]{7}(?=\D|$)|^[0-9]{7}(?=\D|$)?

怎么样:(?<= \ D)[0-9] {7}(?= \ D | $)| ^ [0-9] {7}(?= \ D | $)?

#4


0  

var regex = /(\b|\D)(\d{7})(\b|\D)/g
function get7digits(s) {
    var md, matches=[];
    while( (md=regex.exec(s)) !== null ) matches.push(md[2]);
    return matches;
}

get7digits('q1234567q123456789q123456q1324567q1234567')

["1234567", "1324567"]

[“1234567”,“1324567”]

#5


0  

How about this one:

这个怎么样:

/(^|\D)\d{7}(\$|\D)/gm

/(^ | \ d)\ d {7}(\ $ | \ d)/克

http://regex101.com/r/iT5cR3

http://regex101.com/r/iT5cR3

#1


2  

You could maybe use something like this?

你可以使用这样的东西吗?

var regex = /(?:^|\D)(\d{7})(?!\d)/g;
var s = "q1234567q123456789q123456q1324567q1234567";
var match, matches=[];

while ( (match=regex.exec(s)) !== null ) {
    matches.push(match[1]);
}

alert(matches);

jsfiddle demo

jsfiddle演示

#2


4  

This regex should work:

这个正则表达式应该工作:

/^\D*\d{7}\D*$/

Online Demo: http://regex101.com/r/nE5eI6

UPDATE: As per edited question and comments below you can use this regex:

更新:根据下面编辑的问题和评论,您可以使用此正则表达式:

(?:^|\D)(\d{7})(?=\D|$)

And use matched group #1 for your output.

并使用匹配的组#1作为输出。

Demo: http://regex101.com/r/wL4oW1

#3


0  

What about: (?<=\D)[0-9]{7}(?=\D|$)|^[0-9]{7}(?=\D|$)?

怎么样:(?<= \ D)[0-9] {7}(?= \ D | $)| ^ [0-9] {7}(?= \ D | $)?

#4


0  

var regex = /(\b|\D)(\d{7})(\b|\D)/g
function get7digits(s) {
    var md, matches=[];
    while( (md=regex.exec(s)) !== null ) matches.push(md[2]);
    return matches;
}

get7digits('q1234567q123456789q123456q1324567q1234567')

["1234567", "1324567"]

[“1234567”,“1324567”]

#5


0  

How about this one:

这个怎么样:

/(^|\D)\d{7}(\$|\D)/gm

/(^ | \ d)\ d {7}(\ $ | \ d)/克

http://regex101.com/r/iT5cR3

http://regex101.com/r/iT5cR3