gpt4 book ai didi

javascript - 如何使用嵌套正则表达式

转载 作者:行者123 更新时间:2023-12-01 02:55:00 26 4
gpt4 key购买 nike

我正在尝试转换此内容:
\/\*[^*\/]*(?:(?!\/\*|\*\/)[*\/][^*\/]*)*\*\/g
这是一个完美运行的嵌套正则表达式

/* 一个 */
东西一
/* 两个 /* 三个 */ 两个 */
东西二
/* 四 */



像这样:
a: {[^\}]*(?:(?!a: {|\})[\}][^\}]*)*\}g (无法正常工作)
我已经尝试让它工作好几天了……但没有运气。

a:{一}
东西一
a: { 两个 a: { 三 } 两个 }
东西二
a:{四}


它应该如何工作

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:{一}
东西一
a: { 两个 a: { 三 } 两个 }
东西二
a:{四}


但应该是这样的
a:{一}
东西一
a: { 两个 a: { 三 } 两个 }
东西二
a:{四}

最佳答案

用于捕获最深的巢穴

使用/\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>

关于javascript - 如何使用嵌套正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46751959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com