這真是一個令人費解的模式。我當時非常肯定它行不通,所以我根本沒嘗試。那麼,為什麼它會奏效呢?
目標似乎是替換引號-逗號-引號序列,但僅限於方括號 [ ] 內的內容。某種程度上,正向前瞻實現了這一點。
(?=[^\[\]]*\])
意思是「向前找零個或多個非方括號字符,後面跟著一個右方括號」。
因此,[ 前的引號-逗號-引號無法匹配,因為正規表示式無法在不被 [ 阻擋的情況下找到尾隨的 ]。
最後一個引號(逗號加引號)在 ] 之後無法匹配,因為沒有另一個 ]。
中間的引號-逗號-引號…有效。
還行吧。不過這對正規表示式引擎來說工作量相當大;正向先行斷言需要反覆掃描到字串末尾。我不會讓未來的維護者在沒有註釋的情況下自行摸索,我可能會這樣做:
source = "\[
",":["11000278031.胡剑","11000259239.胡慧","11000426432.叶子","11000445039.张大为","11000469637.詹浏洋","11000480631.李书颖"],","
]\";
Write( source );
findthis = "\!",\!""; // ","
replacewith = "&";
// goal: replace findthis
// with replacethis
// but only inside the [ ... ] square brackets.
If( 0 < Contains( source, "[" ) < Contains( source, "]" ), "ok", Throw( "something wrong here" ));
parts = Words( source, "[]" );
If( N Items( parts ) == 3, "ok", Throw( "this needs more thought" ));
Substitute Into( parts[2], findthis, replacewith );
result = parts[1] || "[" || parts[2] || "]" || parts[3];
Write( result );
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookahead_assertion
謝謝你,克雷格!
實際上,我只想知道如何將這個 JSON 資料轉換成一個完美的表格。
tx="\[[{"title":"AA,AA","BName":"AA","BCode":"605366","orgCode":"82004416","orgName":"AA","orgSName":"AA","publishDate":"2025-12-28 00:00:00.","infoCode":"AP202512281809952250","column":"002004006001","predictNextTwoYearB":"","predictNextTwoYearPe":"","predictNextYearB":"","predictNextYearPe":"","predictThisYearB":"","predictThisYearPe":"","predictLastYearB":"","predictLastYearPe":"","actualLastTwoYearB":"","actualLastYearB":"","industryCode":"","industryName":"","emIndustryCode":"","indvInduCode":"538","indvInduName":"AA","emBCode":"","emBValue":"","emBName":"","lastEmBCode":"","lastEmBValue":"","lastEmBName":"","BChange":"","reportType":2,"author":["11506051.AA"],"indvIsNew":"001","researcher":"AA","newListingDate":"2020-08-12 00:00:00.","newPurchaseDate":"2020-07-31 00:00:00.0","newIssueB":9.98,"newPeIssueA":22.78,"indvAimBT":"","indvAimBL":"","attachType":"0","attachSize":383,"attachPages":3,"encodeUrl":"6XT6d6nxZNcP9JXS1dtw9ivwowNnxWgBjsEKvrMIHoo=","sBName":"","sBCode":"","B":"SHANGHAI","authorID":["11506051"],"count":1,"orgType":"white"},{"title":"AAI,AA","BName":"AA","BCode":"688800","orgCode":"80036717","orgName":"AA","orgSName":"AA","publishDate":"2025-12-28 00:00:00.","infoCode":"AP202512281809913359","column":"002004001003","predictNextTwoYearB":"2.67","predictNextTwoYearPe":"32.97","predictNextYearB":"2.08","predictNextYearPe":"42.24","predictThisYearB":"1.46","predictThisYearPe":"60.44","predictLastYearB":"","predictLastYearPe":"","actualLastTwoYearB":"","actualLastYearB":"","industryCode":"","industryName":"","emIndustryCode":"","indvInduCode":"459","indvInduName":"AA","emBCode":"007","emBValue":"3","emBName":"AA","lastEmBCode":"007","lastEmBValue":"3","lastEmBName":"AA","BChange":3,"reportType":2,"author":["11392732.AA","11524834.AA"],"indvIsNew":"001","researcher":"AA,AA","newListingDate":"2021-07-22 00:00:00.","newPurchaseDate":"2021-07-12 00:00:00.0","newIssueB":15.02,"newPeIssueA":24.49,"indvAimBT":"","indvAimBL":"","attachType":"0","attachSize":764,"attachPages":5,"encodeUrl":"6XT6d6nxZNcP9JXS1dtw9snalC40a3QwRrj9tML8YAw=","sBName":"AA","sBCode":"0301","B":"SHANGHAI","authorID":["11392732","11524834"],"count":1,"orgType":"white"},{"title":"AA,AA","BName":"AA","BCode":"601038","orgCode":"80064","orgName":"AA","orgSName":"AA","publishDate":"2025-12-28 00:00:00.","infoCode":"AP202512281809893801","column":"002004002003","predictNextTwoYearB":"0.9","predictNextTwoYearPe":"15.7600","predictNextYearB":"0.8100","predictNextYearPe":"17.4900","predictThisYearB":"0.7900","predictThisYearPe":"17.9200","predictLastYearB":"","predictLastYearPe":"","actualLastTwoYearB":"","actualLastYearB":"","industryCode":"","industryName":"","emIndustryCode":"","indvInduCode":"910","indvInduName":"AA","emBCode":"007","emBValue":"3","emBName":"AA","lastEmBCode":"007","lastEmBValue":"3","lastEmBName":"AA","BChange":3,"reportType":2,"author":["11379333.AA"],"indvIsNew":"001","researcher":"AA","newListingDate":"2012-08-08 00:00:00.","newPurchaseDate":"2012-07-27 00:00:00.0","newIssueB":5.4,"newPeIssueA":14.44,"indvAimBT":"","indvAimBL":"","attachType":"0","attachSize":321,"attachPages":4,"encodeUrl":"6XT6d6nxZNcP9JXS1dtw9vuL66I7rHatL9vNIud7cP0=","sBName":"AA","sBCode":"0101","B":"SHANGHAI","authorID":["11379333"],"count":1,"orgType":"white"},{"title":"AA:AA,AA","BName":"AA","BCode":"002488","orgCode":"80162","orgName":"AA","orgSName":"AA","publishDate":"2025-12-25 00:00:00.","infoCode":"AP202512251807934364","column":"002004001003","predictNextTwoYearB":"1.0100","predictNextTwoYearPe":"8.4","predictNextYearB":"0.3300","predictNextYearPe":"25.4","predictThisYearB":"0.0600","predictThisYearPe":"131.8","predictLastYearB":"","predictLastYearPe":"","actualLastTwoYearB":"","actualLastYearB":"","industryCode":"","industryName":"","emIndustryCode":"","indvInduCode":"481","indvInduName":"AA","emBCode":"007","emBValue":"3","emBName":"AA","lastEmBCode":"","lastEmBValue":"","lastEmBName":"","BChange":2,"reportType":2,"author":["11250834.AA","11245529.AA","11442637.AA"],"indvIsNew":"001","researcher":"AA,AA,AA","newListingDate":"2010-10-21 00:00:00.","newPurchaseDate":"2010-10-11 00:00:00.0","newIssueB":22,"newPeIssueA":73.33,"indvAimBT":"","indvAimBL":"","attachType":"0","attachSize":2285,"attachPages":19,"encodeUrl":"MKF7cC+MZ9vDOQWsqD7ZgmRE9eUeYHJK9njuGLTcICI=","sBName":"AA(1)","sBCode":"0201","B":"SHENZHEN","authorID":["11250834","11245529","11442637"],"count":1,"orgType":"white"},{"title":"BDAA,AA","BName":"AA","BCode":"688621","orgCode":"80064","orgName":"AA","orgSName":"AA","publishDate":"2025-12-25 00:00:00.","infoCode":"AP202512251807725254","column":"002004006003","predictNextTwoYearB":"2.4600","predictNextTwoYearPe":"28.0","predictNextYearB":"2.2600","predictNextYearPe":"30.5400","predictThisYearB":"1.8","predictThisYearPe":"38.3300","predictLastYearB":"","predictLastYearPe":"","actualLastTwoYearB":"","actualLastYearB":"","industryCode":"","industryName":"","emIndustryCode":"","indvInduCode":"727","indvInduName":"AA","emBCode":"007","emBValue":"3","emBName":"AA","lastEmBCode":"007","lastEmBValue":"3","lastEmBName":"AA","BChange":3,"reportType":2,"author":["11296231.AA","11486715.AA"],"indvIsNew":"001","researcher":"AA,AA","newListingDate":"2021-06-21 00:00:00.","newPurchaseDate":"2021-06-09 00:00:00.0","newIssueB":26.89,"newPeIssueA":29.72,"indvAimBT":"","indvAimBL":"","attachType":"0","attachSize":315,"attachPages":4,"encodeUrl":"MKF7cC+MZ9vDOQWsqD7ZgvFJHAi4XRobbkVOxAB2GK4=","sBName":"AA","sBCode":"0101","B":"SHANGHAI","authorID":["11296231","11486715"],"count":1,"orgType":"white"}]]\";
d1 =jsontodatatable(tx);
這將增加行數。