Match exactly 10 digits with unknown boundaries?

Is there any regex I can use to match blocks of exactly 10 digits? For instance, I have this:

/\d{10}(?!\d+)/g

And this matches 2154358383 when given 2154358383 fine, but is also matches 1213141516 when given 12345678910111213141516, which I don't want.

What I think I need is a look-behind assertion (in addition to my lookahead already in there), that checks to make sure the character preceding the match is not an integer, but I can't figure out how to do that.

I tried

/(?:[^\d]+)\d{10}(?!\d+)/g

But that broke my first match of 2154358383, which is bad.

How can I write this to only match groups of 10 integers (no more, no less) with unknown boundaries?

I should also note that I'm trying to extract these out of a much larger string, so ^ and $ are out of the question.

This should work: ([^\d]|^)\d{10}([^\d]|$)

Could you do something like:

([^\d]|^)(\d{10})([^\d]|$)

In other words, the beginning of the string or a non-digit, ten digits, then the end of the string or a non-digit. That should solve the cases you looked for above.

You can use the regex like this:

var regex = /([^\d]|^)(\d{10})([^\d]|$)/;
var match = regex.exec(s);
var digits = match[2];

This should match numbers at the beginning of the string (the ^) or in the middle/end (the [^\d] and the (?!\d). If you care about the exact match and not just that it matches in the first place, you'll need to grab the first group in the match.

/(?:[^\d]|^)(\d{10})(?!\d)/g

This would be easier if JavaScript regular expressions supported lookbehind.

What about the next?

perl -nle 'print if /(\b|\D)(\d{10})(\D|\b)/' <<EOF
123456789
x123456789
123456789x
1234567890
x1234567890
1234567890x
12345678901
x12345678901
x12345678901x
EOF

will print only

1234567890
x1234567890
1234567890x

Try this

var re = /(?:^|[^\d])(\d{10})(?:$|[^\d])/g

re.exec ( "2154358383")
//["2154358383", "2154358383"]
re.exec ( "12345678910111213141516" )
//null
re.exec ( "abc1234567890def" )
//["c1234567890d", "1234567890"]

val = '1234567890 sdkjsdkjfsl 2234567890 323456789000 4234567890';
re.exec ( val )
//["1234567890 ", "1234567890"]
re.exec ( val )
//[" 2234567890 ", "2234567890"]
re.exec ( val )
//[" 4234567890", "4234567890"]
re.exec ( val )
//null

I know you said "no ^" but maybe it's okay if you use it like this?:

rx = /(?:^|\D)(\d{10})(?!\d)/g

Here's a quick test:

> val = '1234567890 sdkjsdkjfsl 2234567890 323456789000 4234567890'
'1234567890 sdkjsdkjfsl 2234567890 323456789000 4234567890'
> rx.exec(val)[1]
'1234567890'
> rx.exec(val)[1]
'2234567890'
> rx.exec(val)[1]
'4234567890'

Simple with lookbehind:

/(?<!\d)\d{10}(?!\d)/g

i would cheat and do something like

if (myvar.toString().substring(1, 10) = "1234567890") ....

:)