我正在尝试转换这个:
\/\*[^*\/]*(?:(?!\/\*|\*\/)[*\/][^*\/]*)*\*\/g
这是一个完美工作的嵌套Regex。
/* one */
填一
/* 2 /* three */ 2 */
第二种
/* four */
成了这样的东西:
a: {[^\}]*(?:(?!a: {|\})[\}][^\}]*)*\}g (不能正常工作)
几天来,我一直在努力让它发挥作用,…没有运气。
a: { one }
填一
a:{两个a: { three } 2}
第二种
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 } 2}
第二种
a: { four }
,但应该是这个
a: { one }
填一
a:{两个a: { three } 2}
第二种
a: { four }
发布于 2017-10-15 06:00:54
为了捕捉最深的巢穴
使用/\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>
https://stackoverflow.com/questions/46751959
复制相似问题