简体   繁体   中英

Regex to match numbers from large document in Javascript

Trying to create a regex that could match numbers from large document.

Find at least 10 continuous digits (which can go to maximum 15 digits) that could be separated by one or multiple

-
_
\s
(
)
[
] 

Tried-

/(?:((\d([ \-_\s]+?)){5,8}))/

Eg:

1-2-3-4-5-6-7-8-9-0-12-34
1 2 3 4 5 6 7 8 9 0
123-456-789-0
123---456---789---987
12 34 56 78 90
12_ -34_-56--78__90



You may use

/\d(?:[-_\][()\s]*\d){9,14}/g

See the regex demo

Details

  • \\d - a digit
  • (?:[-_\\][()\\s]*\\d){9,14} - 9 to 14 repetitions of
    • [-_\\][()\\s]* - 0 or more repetitions of - , _ , ] , [ , ( , ) or whitespace
    • \\d - a digit.

Note you do not need to escape [ inside a character class, it is parsed as a literal [ in a JS regex. However, ] must be escaped there, otherwise, it will close the character class prematurely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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