Answer the question
In order to leave comments, you need to log in
Is it possible to make a regular expression for unclosed brackets?
The regular will be in js. I use iblize to enter text, and there is syntax highlighting on regular expressions, I would like to just add a regular expression to its source code and not use mutationobserver on the block in which it generates spans. Interested in parentheses only
Answer the question
In order to leave comments, you need to log in
There is no recursion in native javascript regexp. Accordingly, of course, you can write a construction with a limitation of some depth of nesting by hand (although it’s more likely to generate it).
The problem is not new, and bike builders periodically release their versions of regular expressions (Yandex gave me such a link in response to the request "js recursive regex" )
But judging by the context of the question, a non-native regexp will not work for you, so in general, your task is not solvable.
UPD.
about manual generation:
for a nesting depth of 3, I got this Frankenstein:
/^[^()]*(\([^()]*(\([^()]*(\([^()]*\))*[^()]*\))*[^()]*\))*(?<unclosed>\([^()]*(\([^()]*(\([^()]*\))*[^()]*\))*[^()]*)*[^()]*(\([^()]*(\([^()]*(\([^()]*\))*[^()]*\))*[^()]*\))*[^()]*$/
const anySymbols = `[^()]*`
const closedRegexPattern = (depth) => {
if(depth < 1)
return anySymbols
return `\\((${anySymbols}${closedRegexPattern(depth-1)})*${anySymbols}\\)`
}
const unclosedCatcherRegexPattern = (depth) => {
const unclosedPart = `(?<unclosed>\\((${anySymbols}${closedRegexPattern(depth-1)})*${anySymbols})*`
return `^${anySymbols}${closedRegexPattern(depth)}${unclosedPart}${closedRegexPattern(depth)}${anySymbols}$`
}
console.log(unclosedCatcherRegexPattern(150))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question