0

I'm trying to convert this:
\/\*[^*\/]*(?:(?!\/\*|\*\/)[*\/][^*\/]*)*\*\/g
This is a nested Regex that works perfectly

/* one */
Stuff one
/* two /* three */ two */
Stuff two
/* four */



Into something like this:
a: {[^\}]*(?:(?!a: {|\})[\}][^\}]*)*\}g (Does not work properly)
I have been trying to get this to work for days now… no luck.

a: { one }
Stuff one
a: { two a: { three } two }
Stuff two
a: { four }


How it's suppose to work

source = "/* one */ Stuff one /* two  /* three */  two */ Stuff two /* four */"

regex = /\/\*[^*\/]*(?:(?!\/\*|\*\/)[*\/][^*\/]*)*\*\//g

results = source.match(regex)


$("body").append("<pre>" + JSON.stringify(results, null, 2) + "</pre>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

What I've done

source = "a: { one } Stuff one a: { two  a: { three }  two } Stuff two a: { four }"

regex = /a: {[^\}]*(?:(?!a: {|\})[\}][^\}]*)*\}/g

results = source.match(regex)


$("body").append("<pre>" + JSON.stringify(results, null, 2) + "</pre>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

What I've done… is showing this:
a: { one }
Stuff one
a: { two a: { three } two }
Stuff two
a: { four }


But it should be this
a: { one }
Stuff one
a: { two a: { three } two }
Stuff two
a: { four }

Allen Marshall
  • 381
  • 2
  • 10
  • 5
    [*Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.* - Jamie Zawinski](http://regex.info/blog/2006-09-15/247) –  Oct 15 '17 at 05:17
  • [That will never work for the same reason nested tags in html will never work. Not a regular language.](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) –  Oct 15 '17 at 05:18
  • Could you try this tutorial https://tutorialzine.com/2014/12/learn-regular-expressions-in-20-minutes ! – Momin Oct 15 '17 at 05:27
  • You have something that works. Why are you changing it? What goal are you attempting to accomplish by changing it? You say "something like this" (showing something that's invalid syntax), but you don't say what it is about that which defines "like this". – Makyen Oct 15 '17 at 05:31
  • I need a nested regex for my project… Found what I was looking for under stack overflow and I tried to customize it… I did not make the regex. People down voting someone's question for trying to seek an answer makes this place toxic. – Allen Marshall Oct 15 '17 at 05:34
  • It makes it very hard for new coders to like this place. – Allen Marshall Oct 15 '17 at 05:35
  • 1
    You don't "need a nested regexp". You have a problem, that you **think** the solution to might be a nested regexp. Actually, I don't even know what a "nested regexp" is. Is it a regexp with other regexps nested inside? Is it a regexp designed to match nested (recursive) grammars? is it a regexp designed to match only the innermost occurrences? –  Oct 15 '17 at 06:07
  • I do need a nested regexp… That's the whole point of me asking the question. Someone answered the question. Thanks for your comments. – Allen Marshall Oct 15 '17 at 07:03

1 Answers1

2

For capturing the deepest nest

use /\ba: {(?:(?!\ba: {.*}).)*?}/g

source = "a: { one } Stuff one a: { one a: { two a: { three } two} one } a: { one a: { two } one } fdsf dsae a: { one } Stuff two Stuff three a: { one a: { two a: { three a: { four } three} two} one } a: { one }"

regex = /\ba: {(?:(?!\ba: {.*}).)*?}/g
results = source.match(regex)


$("body").append("<pre>" + JSON.stringify(results, null, 2) + "</pre>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



For capturing entire nest

use /\ba: {.*?(?:(?!\ba: {.*}).)*}/g

source = "a: { one } Stuff one a: { one a: { two a: { three } two} one } a: { one a: { two } one } fdsf dsae a: { one } Stuff two Stuff three a: { one a: { two a: { three a: { four } three} two} one } a: { one }"

regex = /\ba: {.*?(?:(?!\ba: {.*}).)*}/g
results = source.match(regex)


$("body").append("<pre>" + JSON.stringify(results, null, 2) + "</pre>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



Has to be considered a nest

use /\ba: {[^}]*\ba: .*?(?:(?!\ba: {.*}).)*}/g

source = "a: { one } Stuff one a: { one a: { two a: { three } two} one } a: { one a: { two } one } fdsf dsae a: { one } Stuff two Stuff three a: { one a: { two a: { three a: { four } three} two} one } a: { one }"

regex = /\ba: {[^}]*\ba: .*?(?:(?!\ba: {.*}).)*}/g
results = source.match(regex)


$("body").append("<pre>" + JSON.stringify(results, null, 2) + "</pre>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



Organize both possible nest and true nests

use /\ba: {\s?[^}]*\ba: {.*?(?:(?!\ba: {.*}).)*}|(\ba: {.*?(?:(?!\ba: {.*}).)*})/g

source = "a: { one } Stuff one a: { one a: { two a: { three } two} one } a: { one a: { two } one } fdsf dsae a: { one } Stuff two Stuff three a: { one a: { two a: { three a: { four} three} two} one } a: { one }"

regex = /\ba: {\s?[^}]*\ba: {.*?(?:(?!\ba: {.*}).)*}|(\ba: {.*?(?:(?!\ba: {.*}).)*})/g
results = source.match(regex)

for (let i = 0; i < results.length; i++) {
  breakdown = regex.exec(source)

  if (breakdown[1]) {
    $(".box1").append(breakdown[1] + '<br>')
  } else {
    $(".box2").append(breakdown[0] + '<br>')
  }

}
.boxes {
  padding: 0px 30px;
  position: absolute;
}

.box2 {
  right: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="boxes box1"></div>
<div class="boxes box2"></div>
Monwell Partee
  • 708
  • 7
  • 16