我正在使用带有VS Code的Nodejs v10.13.0作为我的IDE。
当我运行nodejs程序时, 它在这一行退出:
let matches = address.match(reallyCrazyLongRegex);
在VS Code终端中,然后运行
>> echo %ERRORLEVEL%
-1073741819
请注意,如果我将realCrazyLongRegex替换为realSimpleTestingRegex,它将正常运行。
谢谢, 瑞安
编辑:地址是邮寄地址
正则表达式:
let reallyCrazyLongRegex = new RegExp([arrayOfEveryCityNameInTheUSA].map(function (v)
{
return "([ ]+|^)" + v + "([ ]+|$)";
}).join("|"), "ig");
很简单,很长。
Edit2:我将其拆分为多个正则表达式(每1000个名称) 它在大多数情况下都有效,但会随机崩溃 JavaScript堆内存不足 这根本没有任何意义,因为我正在运行--max-old-space-size = 4048并且我的节点进程从未超过500MB
<---后几个GC --->
[5048:0000000000379B20] 92031 ms:标记扫描293.3(307.6)-> 293.3 (299.8)MB,271.7 / 0.0 ms(平均亩= 0.910,当前亩= 0.000) 请求在旧空间中使用最后的GC [5048:0000000000379B20] 92227 毫秒:标记扫描293.3(299.8)-> 293.3(299.8)MB,195.9 / 0.0毫秒 (平均亩= 0.850,当前亩= 0.000)旧空间中的最后使用GC 请求
<--- JS堆栈跟踪--->
==== JS堆栈跟踪======================================== =
0: ExitFrame [pc: 00000001402BAE3D] 1: StubFrame [pc: 00000001402FB91E] Security context: 0x01abc1840921 <JSObject> 2: match [000001ABC184D011](this=0x00df6da620c1 <String[30]: room no nanjing shanghai china>,0x0352098253f1 <JSRegExp <Very long string[25794]>>) 3: findArea [000001CEE0D6B1E1] [c:\script.js:900]
[bytecode = 0000035209839239 offset = 430](this = 0x00fab1aeb5d1
致命错误:CALL_AND_RETRY_LAST分配失败-JavaScript堆 内存不足1:000000013F6AC80F napi_wrap + 125199 2: 000000013F64BFE6 v8 :: base :: CPU :: has_sse + 36198 3:000000013F64CCB6 v8 :: base :: CPU :: has_sse + 39478 4:000000013FE744AE v8 :: Isolate :: ReportExternalAllocationLimitReached + 94 5: 000000013FE5BC01 v8 :: SharedArrayBuffer :: Externalize + 785 6: 000000013FD22F5C v8 ::内部::堆:: EphemeronKeyWriteBarrierFromCode + 1436 7: 000000013FD1ED69 v8 :: internal :: Heap :: AllocateExternalBackingStore + 1561 8:000000013FD39FEC v8 :: internal :: Factory :: CodeBuilder :: BuildInternal + 412 9: 000000013FD39E26 v8 :: internal :: Factory :: CodeBuilder :: Build + 22 10: 000000013F89D6E4 v8 ::内部:: RegExpMacroAssemblerX64 :: GetCode + 4116 11:000000013FAD5101 v8 :: internal :: IrregexpInterpreter :: MatchInternal + 7057 12: 000000013FAC6C80 v8 ::内部:: RegExpParser ::操作符= + 4544 13: 000000013FAC7149 v8 ::内部:: RegExp :: CompileForTesting + 425 14: 000000013FAC74E5 v8 ::内部:: RegExp :: DotPrintForTesting + 245 15: 000000013FAC7D68 v8 ::内部:: RegExp :: Exec + 1256 16:000000013FAC78F7 v8 ::内部:: RegExp :: Exec + 119 17:000000013FA74B26 v8 :: internal :: DeclarationScope :: was_lazily_parsed + 21974 18: 00000001402BAE3D v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 517453 19:00000001402FB91E v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 782382 20: 00000001402A6E7E v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 435598 21:000000014024675B v8 :: internal :: SetupIsolateDelegate :: SetupHeap + 40555 22: 000000014024675B v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 40555 23:000000014024675B v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 40555 24: 0000000140272F70 v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 222848 25:0000000140297C52 v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 373602 26: 0000000140265354 v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 166500 27:00000001402441AC v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 30908 28: 000000013FD7ED0F v8 ::内部::执行:: CallWasm + 1551 29: 000000013FD7EE03 v8 ::内部::执行:: CallWasm + 1795 30: 000000013FD7F1E5 v8 ::内部::执行:: TryCall + 357 31: 000000013FD61745 v8 ::内部::微任务队列:: Run微任务+501 32: 000000013FE26BE0 v8 :: internal :: Builtins :: builtin_handle + 326608 33: 000000013FE26120 v8 ::内部::建筑物:: builtin_handle + 323856 34: 000000013FE26468 v8 ::内部::建筑物:: builtin_handle + 324696 35: 000000013FE2626E v8 :: internal :: Builtins :: builtin_handle + 324190 36: 00000001402BAF1D v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 517677 37:000000014024675B v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 40555 38: 00000001402442BE v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 31182 39:0000000140243EAC v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 30140 40: 000000013FD7EC82 v8 ::内部::执行:: CallWasm + 1410 41: 000000013FD7E5F9 v8 ::内部::执行::电话+185 42: 000000013FE520E0 v8 :: Function :: Call + 608 43:000000013F6C8E52 节点:: CallbackScope ::〜CallbackScope + 770 44:000000013F6C9114 节点:: CallbackScope ::〜CallbackScope + 1476 45:000000013F6C2093 v8 ::内部::编译器::运算符:: EffectOutputCount + 179 46: 000000013F5BFE33 v8 ::内部::微任务队列::微任务_策略+19395 47:000000013F5C062C v8 :: internal :: MicrotaskQueue :: microtasks_policy + 21436 48: 000000013F5BB6B5 v8 ::内部:: MicrotaskQueue :: microtasks_policy + 1093 49:000000013F6E0A2B uv_tty_set_vterm_state + 8299 50:000000013F6F588C uv_loop_init + 844 51:000000013F6F5BC4 uv_run + 244 52:000000013F6173F3 EVP_CIPHER_CTX_buf_noconst + 29635 53:000000013F6740D0节点::开始+288 54:000000013F52673C RC4_options + 339516 55:0000000140377788 v8 ::内部:: SetupIsolateDelegate :: SetupHeap + 1289880 56: 00000000775A556D BaseThreadInitThunk + 13 57:000000007790385D RtlUserThreadStart + 29
答案 0 :(得分:5)
使用bash找出错误代码:
$ printf "%x" -1073741819
ffffffffc0000005
忽略f
,这是c0000005-访问冲突。
您的正则表达式太复杂了;节点无法应付;它崩溃了。
答案 1 :(得分:3)
应排除许多捕获组。
除非有美国城市的来源每天更新,
做到这一点的方法是
一个完整的 regex trie ,每个月左右都会离线创建。
源#1: US Cities
可在此处获得完整正则表达式的28900个项目列表。
或个别正则表达式按状态细分。
这是从https://simplemaps.com/data/us-cities获得的公共列表中得出的。
有趣的是,52个正则表达式很小且
可以编译成正则表达式列表。
一个代码段可以是这样的:在循环内尝试每个正则表达式
直到找到一个(或多个)状态。
或者,如果已经知道该州,则可以检查城市。
来源2:
Encylopedia Britanica的网站上有大约1950个城市。
这是稀疏地区,也是人口最多的城市。
(\s|^)(A(?:b(?:beville|erdeen|i(?:lene|ng(?:d|t)on))|coma|d(?:a(?:ms)?|rian)|iken|jo|kron|l(?:am(?:eda|o(?:gordo|sa))|b(?:any|ert[\t ]+Lea|uquerque)|coa|exand(?:er[\t ]+City|ria)|hambra|iquippa|l(?:entown|iance)|ma|pine|t(?:a|o(?:n|ona)|us)|va)|m(?:a(?:na[\t ]+Colonies|rillo)|bridge|e(?:ric(?:an[\t ]+Fork|us)|s(?:bury)?)|herst|sterdam)|n(?:a(?:co(?:nda|rtes)|darko|heim)|chorage|d(?:alusia|erson(?:ville)?|over)|n(?:[\t ]+Arbor|apolis|iston)|sonia|tioch)|p(?:alachicola|pleton)|r(?:cadia|dmore|ka(?:delphia|nsas[\t ]+(?:City|Post))|lington(?:[\t ]+Heights)?|t(?:esia|hur))|s(?:bury[\t ]+Park|h(?:e(?:boro|ville)|land|tabula)|pen|toria)|t(?:chison|h(?:ens|ol)|lant(?:a|ic[\t ]+City)|more|tleboro)|u(?:burn|gusta|rora|stin)|vondale)|B(?:a(?:bylon|inbridge|ker(?:[\t ]+City|sfield)|ltimore|ngor|r(?:[\t ]+Harbor|aboo|b(?:erton|ourville)|dstown|nstable|r(?:e|ington)|stow|t(?:lesville|ow))|strop|t(?:avia|esville|h|on[\t ]+Rouge|tle[\t ]+Creek)|y(?:[\t ]+(?:City|Saint[\t ]+Louis)|onne|town))|e(?:a(?:con|trice|u(?:for|mon)t|verton)|ckley|dford|l(?:en|fast|l(?:e(?:[\t ]+(?:Fourch|Glad)e|fontaine|v(?:ill|u)e)|ingham|ows[\t ]+Falls)|mont|oit|videre)|midji|n(?:d|nington|ton(?:[\t ]+Harbor)?)|r(?:ea|keley|lin)|ssemer|th(?:any|esda-Chevy[\t ]+Chase|lehem)|verly(?:[\t ]+Hills)?)|i(?:ddeford|g[\t ]+Spring|l(?:lings|oxi)|nghamton|rmingham|s(?:bee|marck))|l(?:a(?:ckfoot|irsville)|oom(?:field(?:[\t ]+Hills)?|ington|sburg)|uefield|ytheville)|o(?:ca[\t ]+Raton|galusa|ise|nners[\t ]+Ferry|o(?:n(?:e(?:sborough)?|ville)|thbay[\t ]+Harbor)|r(?:dentown|ger)|s(?:sier[\t ]+City|ton)|u(?:lder(?:[\t ]+City)?|n(?:d[\t ]+Brook|tiful)|rne)|w(?:ie|ling[\t ]+Green)|ys[\t ]+Town|zeman)|r(?:a(?:d(?:enton|ford)|in(?:erd|tree)|n(?:ford|son)|ttleboro)|e(?:a|ckenridge|merton)|i(?:dge(?:port|ton)|gh(?:am[\t ]+City|ton)|stol)|o(?:ckton|nx|ok(?:field|ings|l(?:ine|yn))|wnsville)|unswick|yan)|u(?:ckhannon|ena[\t ]+Park|ffalo|r(?:bank|lington|ns)|tte))|C(?:a(?:dillac|hokia|iro|l(?:ais|dwell|exico|houn|istoga|umet[\t ]+City)|m(?:bridge|den|pbellsville)|n(?:on[\t ]+City|ton|yon)|pe[\t ]+(?:Coral|Girardeau|May)|r(?:bondale|ibou|l(?:i(?:nvil|s)le|sbad)|mel|rollton|son[\t ]+City|thage)|s(?:a[\t ]+Grande|per|tine)|tonsville)|e(?:dar[\t ]+(?:City|Falls|Rapids)|ntral(?:[\t ]+(?:City|Falls)|ia))|h(?:a(?:dron|m(?:bersburg|paign)|n(?:dler|ute)|pel[\t ]+Hill|rl(?:e(?:s(?:[\t ]+(?:City|Town)|to(?:w)?n)|voix)|otte(?:sville)?)|ttanooga|utauqua)|e(?:boygan|ektowaga|l(?:msford|sea)|rokee|s(?:apeake|ter)|yenne)|i(?:c(?:ago(?:[\t ]+Heights)?|kas(?:aw|ha)|o(?:pee)?)|llicothe)|ula[\t ]+Vista)|i(?:cero|ncinnati)|l(?:a(?:nton|r(?:emo(?:nt|re)|ks(?:burg|dale|ville))|yton)|e(?:ar(?:field|water)|burne|veland(?:[\t ]+Heights)?)|i(?:fton|max|nton)|ovis)|o(?:coa(?:[\t ]+Beach|-Rockledge)|dy|eur[\t ]+d’Alene|ffeyville|h(?:asset|oes)|l(?:l(?:ege[\t ]+(?:Park|Station)|insville)|orado[\t ]+Springs|umb(?:ia|us))|mpton|n(?:cord|ey[\t ]+Island|ne(?:aut|rsville)|way)|o(?:keville|perstown|s[\t ]+Bay)|r(?:al[\t ]+Gables|dova|inth|ning|ona(?:do)?|pus[\t ]+Christi|t(?:ez|land)|vallis|ydon)|sta[\t ]+Mesa|u(?:lee[\t ]+Dam|ncil[\t ]+(?:Bluffs|Grove)|peville)|v(?:entry|ington))|r(?:a(?:n(?:ford|ston)|wfordsville)|ipple[\t ]+Creek|o(?:okston|ssett|wn[\t ]+Point)|ystal[\t ]+City)|u(?:l(?:lman|ver[\t ]+City)|mberland|s(?:hing|ter)|yahoga[\t ]+Falls))|D(?:a(?:hlonega|l(?:las|ton|y[\t ]+City)|n(?:bury|v(?:ers|ille))|r(?:ien|lington|tmouth)|v(?:enport|is)|yton(?:a[\t ]+Beach)?)|e(?:[\t ]+(?:Land|Smet)|Kalb|a(?:dwood|rborn)|catur|dham|erfield[\t ]+Beach|fiance|l(?:[\t ]+Rio|aware|ray[\t ]+Beach|ta)|m(?:ing|opolis)|n(?:ison|nis|ton|ver)|r(?:b|r)y|s[\t ]+(?:Mo|Pla)ines|troit|vils[\t ]+Lake)|i(?:ckins|ll|x)on|o(?:dge[\t ]+City|than|uglas|ver|wney)|u(?:buque|luth|n(?:can|kirk)|r(?:an(?:go|t)|ham)|xbury))|E(?:a(?:gle[\t ]+Pass|st(?:[\t ]+(?:Aurora|C(?:hicago|leveland)|Greenwich|Ha(?:mpton|rtford|ven)|L(?:ansing|iverpool)|Moline|Orange|P(?:oint|rovidence)|Saint[\t ]+Louis)|chester|ham|on|po(?:inte|rt))|u[\t ]+Claire)|corse|d(?:enton|gartown|i(?:nburg|son)|mond)|ffingham|l(?:[\t ]+(?:Ce(?:ntr|rrit)o|Dorado|Monte|Paso|Reno)|gin|izabeth(?:[\t ]+City|to(?:w)?n)?|k(?:[\t ]+City|hart|ins|o|ton)|l(?:ensburg|sworth)|m(?:hurst|ira)|wood|y(?:ria)?)|m(?:mitsburg|poria)|n(?:field|glewood|id|terprise)|phrata|rie|s(?:c(?:anaba|ondido)|sex|t(?:es[\t ]+Park|herville))|u(?:clid|faula|gene|reka)|v(?:ans(?:ton|ville)|e(?:leth|rett))|x(?:celsior[\t ]+Springs|eter))|F(?:a(?:ir(?:banks|f(?:ax|ield)|haven|mont)|l(?:l(?:[\t ]+River|on|s[\t ]+Church)|mouth)|r(?:go|ibault|mington)|yetteville)|er(?:gus(?:[\t ]+Falls|on)|nandina[\t ]+Beach)|i(?:llmore|ndlay|t(?:chburg|zgerald))|l(?:agstaff|int|or(?:ence|issant)|ushing)|o(?:n(?:d[\t ]+du[\t ]+Lac|tana)|r(?:est[\t ]+Hills|rest[\t ]+City|t[\t ]+(?:Benton|Collins|Dodge|Kent|L(?:auderdal|e)e|M(?:organ|yers)|P(?:ayn|ierc)e|S(?:cott|mith)|Valley|W(?:a(?:lton[\t ]+Beach|yne)|orth))))|r(?:a(?:mingham|nk(?:fort|lin))|e(?:d(?:erick(?:sburg)?|onia)|eport|mont|nch[\t ]+Lick|sno))|ul(?:ler)?ton)|G(?:a(?:dsden|ffney|inesville|l(?:e(?:na|sburg)|l(?:atin|ipolis|up)|veston)|r(?:d(?:en[\t ]+(?:City|Grove)|iner)|land|y)|stonia|tlinburg)|e(?:n(?:ev|o)a|orgetown|rmantown|ttysburg)|il(?:a[\t ]+Bend|lette)|l(?:assboro|en(?:[\t ]+Ellyn|d(?:al|iv)e|s[\t ]+Falls|view|wood[\t ]+Springs)|o(?:be|ucester|versville))|o(?:l(?:d(?:en|field|sboro)|iad)|shen)|r(?:a(?:fton|n(?:d[\t ]+(?:Forks|Haven|Island|Junction|Rapids)|ite[\t ]+City|ts(?:[\t ]+Pass)?)|yling)|e(?:at[\t ]+(?:B(?:arrington|end)|Falls|Neck)|e(?:ley|n(?:[\t ]+(?:Bay|River)|belt|eville|field|sb(?:oro|urg)|ville|w(?:ich|ood)))|nada|tna)|innell|o(?:sse[\t ]+Pointe|ton))|u(?:ilford|lfport|n(?:nison|tersville)|thrie|ymon))|H(?:a(?:ckensack|ddonfield|gerstown|ines|l(?:ifax|landale[\t ]+Beach)|m(?:den|ilton|mond(?:sport)?|pton)|n(?:alei|cock|nibal|over)|r(?:l(?:an|em|ingen)|mony|pers[\t ]+Ferry|r(?:is(?:burg|on)|odsburg)|t(?:ford|sville)|wich)|stings|ttiesburg|v(?:erhill|re)|y(?:s|ward)|z(?:ard|leton))|e(?:ber[\t ]+City|lena|mpstead|nderson|r(?:kimer|rin|shey))|i(?:aleah|bbing|ckory|gh(?:[\t ]+Point|land[\t ]+Park)|l(?:lsboro(?:ugh)?|o)|n(?:gham|ton))|o(?:b(?:art|bs|oken)|dgenville|l(?:denville|l(?:and|y(?:[\t ]+Springs|wood))|yoke)|me(?:r|stead)|n(?:aunau|esdale|olulu)|od[\t ]+River|p(?:e(?:well)?|kinsville)|quiam|t[\t ]+Springs|u(?:ghton|lton|ma|ston))|u(?:dson|go|nt(?:ington(?:[\t ]+Beach)?|sville)|ron|tchinson)|y(?:a(?:nnis|ttsville)|de[\t ]+Park))|I(?:daho[\t ]+(?:City|Falls)|lion|n(?:d(?:ependence|i(?:an(?:a(?:polis)?|ola)|o))|glewood|ter(?:lochen|national[\t ]+Falls))|owa[\t ]+City|pswich|r(?:on(?:[\t ]+Mountain|wood)|vin(?:e|g(?:ton)?))|shpeming|thaca)|J(?:a(?:ckson(?:ville)?|mestown|nesville|sper)|e(?:annette|fferson(?:[\t ]+City|ville)|rsey[\t ]+City)|im[\t ]+Thorpe|o(?:hn(?:[\t ]+Day|s(?:on[\t ]+City|town))|liet|nesboro(?:ugh)?|plin)|un(?:ction[\t ]+City|eau))|K(?:a(?:hului|l(?:amazoo|ispell)|n(?:ab|eohe|kakee|sas[\t ]+City)|paa|skaskia|waihae)|e(?:arney|ene|l(?:logg|so)|n(?:ne(?:bunkport|wick)|osha|t)|okuk|t(?:chikan|tering)|wanee|y(?:[\t ]+West|ser))|i(?:l(?:gore|leen)|n(?:g(?:man|s(?:port|ton|ville))|ston)|rksville|tt(?:ery|y[\t ]+Hawk))|lamath[\t ]+Falls|noxville|o(?:diak|komo|tzebue))|L(?:a(?:[\t ]+(?:Crosse|Gran(?:d|g)e|Habra|Junta|Salle)|c(?:kawann|oni)a|fayette|guna[\t ]+Beach|haina|ie|ke(?:[\t ]+(?:C(?:harles|ity)|Forest|Geneva|Havasu[\t ]+City|Oswego|Placid|Wales)|hurst|land|view|wood)|mar|n(?:caster|der|sing)|r(?:amie|edo|go)|s[\t ]+(?:Cruce|Vega)s|urel|w(?:rence|ton)|yton)|e(?:a(?:d(?:ville)?|venworth)|banon|hi|nox|ominster|vittown|w(?:es|is(?:burg|to(?:w)?n))|xington)|i(?:ber(?:al|tyville)|ma|ncoln|sle|t(?:chfield|tle(?:[\t ]+(?:Falls|Rock)|ton))|v(?:ermore|ingston|onia))|o(?:ck(?:[\t ]+Haven|port)|di|gan|m(?:bard|poc)|ng(?:[\t ]+B(?:ea|ran)ch|mont|view)|rain|s[\t ]+A(?:lamo|ngele)s|uisville|v(?:eland|ington)|we(?:ll|r[\t ]+Southampton))|u(?:b(?:bock|ec)|d(?:ington|low)|fkin|mberton)|yn(?:chburg|n))|M(?:a(?:c(?:hias|kinaw[\t ]+City|o(?:mb|n))|dison|gnolia|l(?:den|ibu)|maroneck|n(?:assas|chester|dan|hattan|i(?:stee|towoc)|kato|sfield|ti)|r(?:blehead|i(?:etta|nette|on)|lborough|quette|shall|tin(?:ez|s(?:[\t ]+Ferry|burg|ville))|y(?:s)?ville)|s(?:on[\t ]+City|s(?:ena|illon))|ttoon|y(?:field|sville))|c(?:Al(?:ester|len)|Cook|K(?:eesport|inney)|Minnville|Pherson)|e(?:adville|d(?:ford|icine[\t ]+Lodge)|lbourne|mphis|n(?:asha|lo[\t ]+Park|ominee|tor)|r(?:ced|id(?:e|ia)n)|s(?:a|quite)|xico)|i(?:ami(?:[\t ]+Beach)?|chigan[\t ]+City|d(?:dle(?:bury|sboro|town)|land|west[\t ]+City)|l(?:an|bank|es[\t ]+City|ford|l(?:burn|edgeville|ville)|ton|waukee)|n(?:den|eola|neapolis|ot)|s(?:hawaka|s(?:ion|oula))|tchell)|o(?:ab|b(?:il|ridg)e|desto|line|n(?:ett|mouth|roe(?:ville)?|t(?:clair|erey|gomery|icello|pelier|rose))|or(?:e|head)|r(?:ehead[\t ]+City|gan(?:[\t ]+City|to(?:w)?n)|ri(?:lto|stow)n)|s(?:cow|es[\t ]+Lake)|un(?:dsville|t(?:[\t ]+(?:Clemens|Holly|Pleasant|Vernon)|ain[\t ]+View)))|u(?:n(?:cie|delein)|r(?:freesboro|ray)|s(?:catine|k(?:egon|ogee)))|y(?:rtle[\t ]+Beach|stic))|N(?:a(?:cogdoches|gs[\t ]+Head|hant|mpa|nticoke|p(?:a|erville|les|panee)|rragansett|sh(?:ua|ville)|t(?:ch(?:ez|itoches)|ick)|u(?:gatuck|voo))|e(?:braska[\t ]+City|e(?:dles|nah)|osho|phi|w(?:[\t ]+(?:Albany|B(?:e(?:dford|rn)|r(?:aunfels|itain|unswick))|Castle|Glarus|H(?:a(?:rmony|ven)|ope)|Iberia|Kensington|London|M(?:a(?:drid|r(?:ket|tinsville))|ilford)|Orleans|P(?:altz|hiladelphia)|Rochelle|Smyrna[\t ]+Beach|Ulm|Windsor|York[\t ]+City)|ark|b(?:erg|ur(?:gh|yport))|castle|port(?:[\t ]+(?:Beach|News))?|ton))|i(?:agara[\t ]+Fall|le)s|o(?:gales|me|r(?:folk|ma[ln]|ris(?:town)?|t(?:h(?:[\t ]+(?:Adams|C(?:hicago|ollege[\t ]+Hill)|H(?:aven|empstead)|Kingstown|L(?:as[\t ]+Vegas|ittle[\t ]+Rock)|Platte)|ampton|field)|on)|w(?:alk|ich|ood))|vato)|yack)|O(?:ak(?:[\t ]+(?:Harbor|Park|Ridge)|land)|berlin|c(?:ala|ean(?:[\t ]+(?:City|Springs)|side)|onto)|dessa|gden(?:sburg)?|il[\t ]+City|jai|k(?:lahoma[\t ]+City|mulgee)|l(?:athe|d[\t ]+Saybrook|ean|ympia)|maha|n(?:e(?:id|ont)a|tario)|pel(?:ika|ousas)|r(?:a(?:ibi|nge(?:burg)?)|derville|e(?:gon(?:[\t ]+City)?|m)|lando|mond[\t ]+Beach|o(?:no|ville))|s(?:awatomie|ceola|hkosh|kaloosa|sining|wego)|tt(?:a|um)wa|uray|verland[\t ]+Park|w(?:atonna|ensboro)|x(?:fo|na)rd|yster[\t ]+Bay|zark)|P(?:a(?:cific[\t ]+Grove|ducah|gosa[\t ]+Springs|inesville|l(?:at(?:ine|ka)|m(?:[\t ]+(?:B(?:ay|each)|Springs)|dale|er|yra)|o[\t ]+Alto)|mpa|n(?:ama[\t ]+City|guitch)|r(?:is|k(?:[\t ]+(?:City|Forest|Ridge)|ersburg)|ma|sippany–Troy[\t ]+Hills)|s(?:adena|c(?:agoula|o)|s(?:[\t ]+Christian|aic))|terson|uls[\t ]+Valley|w(?:huska|tucket)|yson)|e(?:abody|cos|ekskill|kin|n(?:dleton|sacola)|oria|r(?:ry|th[\t ]+Amboy|u)|shtigo|t(?:aluma|er(?:borough|sburg)|oskey))|h(?:arr|enix[\t ]+City|il(?:adelphia|ippi)|oenix(?:ville)?)|i(?:erre|ne(?:[\t ]+Bluff|hurst)|pestone|qua|tts(?:burgh?|field))|l(?:a(?:in(?:field|s|view)|no|tts(?:burg|mout)h)|ymouth)|o(?:catello|int[\t ]+(?:Pleasant|Roberts)|m(?:ona|pano[\t ]+Beach)|n(?:ca[\t ]+City|tiac)|rt(?:[\t ]+(?:A(?:ngeles|rthur)|Gibson|Hu(?:eneme|ron)|Lavaca|Orford|Washington)|a(?:ge|les)|land|smouth)|t(?:sdam|ts(?:town|ville))|ughkeepsie|well)|r(?:airie[\t ]+du[\t ]+Chien|es(?:cott|que[\t ]+Isle)|i(?:c(?:e|hard)|est[\t ]+River|n(?:ceton|eville))|ov(?:i(?:dence|ncetown)|o)|yor)|u(?:eblo|llman|t-in-Bay|yallup))|Qu(?:eens|incy)|R(?:a(?:cine|leigh|n(?:cho[\t ]+Cucamonga|dolph|toul)|pid[\t ]+City|ton|wlins)|e(?:ading|d(?:[\t ]+(?:Bluff|Cloud|Wing)|ding|lands|mond|ondo[\t ]+Beach|wood[\t ]+City)|edsport|n(?:o|sselaer|ton)|ston|vere|xburg)|hinelander|i(?:ch(?:ardson|land|mond)|dgewood|pon|ver(?:[\t ]+Forest|side|ton))|o(?:anoke|c(?:hester|k(?:[\t ]+(?:Hill|Island|Springs)|ford|land|ville|y[\t ]+Mount))|gers|lla|m(?:e|ney)|s(?:e(?:burg|lle|ville)|well)|tterdam|yal[\t ]+Oak)|u(?:gby|mford|ston|t(?:herfor|lan)d)|ye)|S(?:a(?:c(?:rament)?o|g(?:[\t ]+Harbor|inaw)|int(?:[\t ]+(?:A(?:lbans|ugustine)|C(?:harles|loud)|George|Ignace|Jo(?:hnsbury|seph)|Louis|Mar(?:tinville|ys[\t ]+City)|P(?:aul|etersburg))|e[\t ]+Genevieve)|l(?:em|i(?:nas?|sbury)|lisaw|t[\t ]+Lake[\t ]+City)|n(?:[\t ]+(?:An(?:gel|toni)o|Bernardino|Clemente|Diego|F(?:e(?:lipe|rnando)|rancisco)|Gabriel|J(?:ose|uan[\t ]+Capistrano)|L(?:eandr|uis[\t ]+Obisp)o|Ma(?:r(?:cos|ino)|teo)|Pedro|Rafael|Simeon)|d(?:[\t ]+Springs|usky|wich)|ford|ta[\t ]+(?:Ana|Barbara|C(?:la(?:r(?:it)?a|us)|ruz)|Fe|Monica|Rosa))|pulpa|ra(?:nac[\t ]+Lake|sota|toga[\t ]+Springs)|u(?:gus|k[\t ]+Centre|lt[\t ]+Sainte[\t ]+Marie|salito)|vannah)|c(?:ar(?:borough|sdale)|henectady|otts(?:boro|dale)|ranton)|e(?:a(?:rcy|side|ttle)|bring|dalia|lma|minole|neca[\t ]+Falls|ward|ymour)|h(?:a(?:ker[\t ]+Heights|mokin|ron|wnee(?:town)?)|e(?:boygan|ffield|l(?:by(?:ville)?|ton)|pherdstown|r(?:id|m)an)|iprock|reveport)|i(?:dney|erra[\t ]+Vista|lver(?:[\t ]+(?:City|Spring)|ton)|m(?:i[\t ]+Valle|sbur)y|oux[\t ]+(?:City|Falls)|tka)|k(?:agway|okie)|m(?:ith[\t ]+Center|yrna)|o(?:corro|mer(?:sworth|ville)|noma|uth(?:[\t ]+(?:Bend|Charleston|H(?:adley|olland)|Kingstown|Orange[\t ]+Village|Sa(?:int[\t ]+Paul|n[\t ]+Francisco))|ampton|ington))|p(?:a(?:nish[\t ]+Fork|r(?:ks|tanburg))|earfish|okane|ring(?:[\t ]+Green|field|ville))|t(?:a(?:mford|rkville|te(?:[\t ]+College|n[\t ]+Island)|unton)|e(?:amboat[\t ]+Springs|rling|ubenville|vens[\t ]+Point)|illwater|o(?:ck(?:bridge|ton)|n(?:ington|y[\t ]+(?:Brook|Point))|ughton)|r(?:atford|eator|oudsburg)|u(?:r(?:bridge|g(?:eon[\t ]+Bay|is))|ttgart))|u(?:dbury|ffolk|m(?:m(?:ersville|it)|ter)|n(?:[\t ]+Valley|bury|nyvale)|perior|sanville)|w(?:arthmore|eetwater)|y(?:lacauga|racuse))|T(?:a(?:coma|hlequah|koma[\t ]+Park|lla(?:dega|hassee)|m(?:aqu|p)a|os|r(?:pon[\t ]+Springs|rytown)|unton)|e(?:lluride|mp(?:l)?e|n[\t ]+Sleep|rre[\t ]+Haute|wksbury|xa(?:rkana|s[\t ]+City))|h(?:e(?:[\t ]+(?:Dalles|Village)|rmopolis)|ibodaux|ousand[\t ]+Oaks)|i(?:conderoga|ffin|llamook|tusville|verton)|o(?:ccoa|ledo|mbstone|nawanda|oele|peka|rr(?:ance|ington)|towa|wson)|r(?:averse[\t ]+City|enton|inidad|oy|u(?:ro|th[\t ]+or[\t ]+Consequences))|u(?:c(?:son|umcari)|l(?:lahom|s)a|pelo|rlock|s(?:c(?:aloos|umbi)a|kegee))|win[\t ]+Falls|yler)|U(?:kiah|nion(?:[\t ]+City|town)?|rbana|tica|valde)|V(?:a(?:il|l(?:d(?:ez|osta)|le(?:jo|y[\t ]+City)|paraiso)|n(?:[\t ]+Buren|couver|dalia))|e(?:n(?:ice|tura)|r(?:million|nal))|i(?:c(?:ksburg|tor(?:ia|ville))|n(?:cennes|eland|ita)|r(?:den|ginia(?:[\t ]+(?:Beach|City))?)|salia))|W(?:a(?:bash|co|h(?:iawa|peton)|i(?:luku|mea)|l(?:l(?:a[\t ]+Walla|ingford)|nut[\t ]+Creek|pi|senburg)|r(?:m[\t ]+Springs|ner[\t ]+Robins|ren(?:sburg)?|wick)|shington|t(?:er(?:bury|ford|loo|town|v(?:ille|liet))|kins[\t ]+Glen|ts)|u(?:ke(?:gan|sha)|sau|watosa)|y(?:cross|ne(?:sboro)?))|e(?:atherford|bster(?:[\t ]+City)?|ehawken|irton|l(?:ch|l(?:esley|fleet|sburg))|natchee|st(?:[\t ]+(?:Allis|B(?:end|ridgewater)|C(?:hester|ovina)|Des[\t ]+Moines|Ha(?:rtford|ven)|Lafayette|Memphis|New[\t ]+York|Orange|P(?:alm[\t ]+Beach|lains|oint)|S(?:eneca|pringfield))|erly|field|minster|on|port)|thersfield|woka|ymouth)|h(?:e(?:aton|eling)|it(?:e[\t ]+(?:Plains|S(?:p|ulphur[\t ]+Sp)rings)|man|tier))|i(?:c(?:hita(?:[\t ]+Falls)?|kford)|l(?:kes-Barre|l(?:i(?:ams(?:burg|on|port|town)|mantic|ngboro|ston)|mar)|m(?:ette|ington)|son)|n(?:chester|d(?:ham|ow[\t ]+Rock|sor(?:[\t ]+Locks)?)|ne(?:mucc|tk)a|o(?:na|oski)|s(?:low|t(?:ed|on-Salem))|ter[\t ]+(?:Haven|Park))|sconsin[\t ]+Dells)|o(?:burn|o(?:d(?:[\t ]+River|bridge|land|s(?:[\t ]+Hole|tock)|ward)|nsocket|ster)|r(?:cester|land|thington))|yandotte)|Xenia|Y(?:a(?:kima|nkton|zoo[\t ]+City)|ellow[\t ]+Springs|o(?:nkers|r(?:ba[\t ]+Linda|k)|ungstown)|psilanti|sleta|u(?:ba[\t ]+City|ma))|Z(?:anesville|ion))(?!\S)
这大约在3毫秒内匹配了所有1960个城市,并且大小只有14k。
https://regex101.com/r/qVcHf6/1
这些正则表达式作为三元树非常快。
您也可以执行此操作,它是使用此Ternary Trie Tool
生成的答案 2 :(得分:1)
我的猜测是,您复杂的正则表达式对您的node.js服务器造成了“重做”。由于node.js是单线程的,因此它尝试使用过多的CPU来解析您的regex表达式,这会在一段时间后破坏节点执行。
您可以通过不使用自己的正则表达式来避免这种情况,并尝试使用一些众所周知的正则表达式,或者如果要进行验证,则使用类似Joi或validator.js
以下是有关“重做”的内容:
https://blog.malwarebytes.com/security-world/technology/2018/08/explained-regular-expression-regex/
或
https://javascript.info/regexp-catastrophic-backtracking
希望这会有所帮助:)