Closed gricha2380 closed 6 years ago
Current promising solution: https://stackoverflow.com/questions/2295657/return-positions-of-a-regex-match-in-javascript
Placing contents into a while loop has presented a few challenges though. At first my code was caught in infinite loop when invoked by removeListener()
. After noting the issue didn't happen when matchEnemies()
is called by processCards()
I realized it was caused by the function returning a blank string, so I put a conditional check for that.
It now almost works. The current remaining problem is with optional quantifier. When using that it returns no matches, although in reality the match result should be identical to no quantifier. E.g.: /[a-e]?/.exec("hello world")
currently returns "", whereas the expected output is "ed".
Still struggling with quantifiers. Challenge documented here: https://repl.it/repls/ConcernedSlimEmulation
Update: I noticed that Regexr does display a warning next to the expression noting it can lead to infinite match. There's a strong possibility this has something to do with the issues I'm seeing.
I'll move forward with what I have. I'm still unable to get .exec()
to match with the ?
quantifier. I'll file it as a separate bug, and I'll reference back to 83d12358 which is the last commit with the old structure for future reference.
.exec()
is working as designed, so this is being closed. The final outstanding issue was an off-by-one bug was caused by the initial run of enemiesDefault
containing blank spaces. A quick \s regex fixed that. I also corrected/simplified the currentMatch
enemyHolder
check.
The ongoing concern of empty matches and infinite loops with optional quantifier can be tracked here: https://github.com/gricha2380/regex/issues/36
The current problem is
matchEnemies()
returns an array of matched characters but does not show where they fall in the parent string. The existing code performs a forEach and looks only for character matches. This is inaccurate and can lead to false positives and is not suited for complex string matching. The goal is to find away to return the index range along with each regex match.