我正在尝试转换此内容:
\/\*[^*\/]*(?:(?!\/\*|\*\/)[*\/][^*\/]*)*\*\/g
这是一个完美的嵌套Regex
/* one */
东西一
/ *两个/* three */
两个* /
东西两个
/* four */
进入类似的事情:
a: {[^\}]*(?:(?!a: {|\})[\}][^\}]*)*\}g
(不能正常工作)
我一直试图让这个工作好几天......没有运气。
a: { one }
东西一
a:{两a: { three }
两个}
东西两个
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>
我所做的......显示了这一点:
a: { one }
东西一
a: { two a: { three }
两个}
东西两个
a: { four }
但应该是这个
a: { one }
东西一
a:{两a: { three }
两个}
东西两个
a: { four }
答案 0 :(得分:2)
使用/\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>
使用/\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>
使用/\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>
使用/\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>