Hello, thank you for your continuous support.
I am writing a program that uses regular expressions in javascript, but the program doesn't work.
When I tried it, the following code encountered the same situation:
let regex=/(\w+(,|\s)*)+;/;
let text = "XXXX, XXXX, XXXX, XXXX, XXXX, XXXX, XXXX, XXXX, XXXX, XXXX";
// Freeze here
let matched = text.match(regex);
console.log (matched [0]);
In the case of this code, text does not end in semicolon, so I thought there was no match and matched would be null, but the program did not complete.
Could you tell me why this happened?
Thank you for your cooperation.
javascript node.js regular-expression
There must be a combination explosion.
regex=/(\w+(,|\s)*)+;/
In , null
with or without , but because it is at the end of the regex expression, the computer creates a pattern for the previous
(\w+(,|\s)*)+
part, and then considers the pattern to see if it matches the given string for the first time.
In particular, (,|\s)*
may or may not have a word delimiter, which is one of the reasons for the combination explosion.(,|\s)+
always determines a partial match for each XXXX,
.So I would do this myself:
let regex=/(\w+(,|\s)+)+(\w+);/;
When I tried ideone, it ended successfully with null.
578 Understanding How to Configure Google API Key
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.