簡體   English   中英

如何使用嵌套RegEx

[英]How to use Nested RegEx

我正在嘗試將其轉換為:
\\/\\*[^*\\/]*(?:(?!\\/\\*|\\*\\/)[*\\/][^*\\/]*)*\\*\\/g
這是一個嵌套的正則表達式,可以完美地工作

/* 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 }

捕捉最深的巢

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM