Reputation: 391
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 }
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>
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 }
Upvotes: 0
Views: 277
Reputation: 718
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>
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>
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>
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>
Upvotes: 2