簡體   English   中英

正則表達式追加字符並刪除結束雙引號

[英]Regex to append character and remove end double quotes

我試過下面的正則表達式來捕獲它的一些字符串:

    "CallbackFnCreate_[\W]{4}(.*?[^\w-])

Required Output should be : /TestResult_20190604-120620 (Capture 
TestResult_20190604-120620 and append "/" in the beginning)

我的第1組輸出是:TestResult_20190604-120620“

1|#||4|3952|updatePanel|WorkflowFileExplorer_ajaxPanel|<div `id="WorkflowFileExplorer_pbContainer" style="width:0px;height:0px;overflow:hidden;position:relative;"><input type="button" name="WorkflowFileExplorer$pb" value="pb" onclick="javascript:__doPostBack(&#39;WorkflowFileExplorer$pb&#39;,&#39;&#39;)" id="WorkflowFileExplorer_pb" tabindex="-1" /><input name="WorkflowFileExplorer$postbackArgument" type="hidden" id="WorkflowFileExplorer_postbackArgument" value="CallbackFnCreate_/***TestResult_20190604-120620" /></div><div id="WorkflowFileExplorer_tree" class="RadTreeView RadTreeView_Default">`

我的猜測是我們希望簡單地做一個替換並略微修改原始表達式:

.+("CallbackFnCreate_\/\*\*\*(.+?)").+

我們期望的輸出在這個捕獲組中:

(.+?)

測試

 const regex = /.+("CallbackFnCreate_\\/\\*\\*\\*(.+?)").+/gm; const str = `1|#||4|3952|updatePanel|WorkflowFileExplorer_ajaxPanel|<div \\`id="WorkflowFileExplorer_pbContainer" style="width:0px;height:0px;overflow:hidden;position:relative;"><input type="button" name="WorkflowFileExplorer\\$pb" value="pb" onclick="javascript:__doPostBack(&#39;WorkflowFileExplorer\\$pb&#39;,&#39;&#39;)" id="WorkflowFileExplorer_pb" tabindex="-1" /><input name="WorkflowFileExplorer\\$postbackArgument" type="hidden" id="WorkflowFileExplorer_postbackArgument" value="CallbackFnCreate_/***TestResult_20190604-120620" /></div><div id="WorkflowFileExplorer_tree" class="RadTreeView RadTreeView_Default">\\` `; const subst = `\\/$2`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result); 

只需使用括號捕獲相關部分並在替換時附加“/”。

 var re = /.+"CallbackFnCreate_\\/\\W{3}([^"]+).+/; var text = '1|#||4|3952|updatePanel|WorkflowFileExplorer_ajaxPanel|<div `id="WorkflowFileExplorer_pbContainer" style="width:0px;height:0px;overflow:hidden;position:relative;"><input type="button" name="WorkflowFileExplorer$pb" value="pb" onclick="javascript:__doPostBack(&#39;WorkflowFileExplorer$pb&#39;,&#39;&#39;)" id="WorkflowFileExplorer_pb" tabindex="-1" /><input name="WorkflowFileExplorer$postbackArgument" type="hidden" id="WorkflowFileExplorer_postbackArgument" value="CallbackFnCreate_/***TestResult_20190604-120620" /></div><div id="WorkflowFileExplorer_tree" class="RadTreeView RadTreeView_Default">`'; var res = text.replace(re, '/$1') console.log(res) 

您可以在替換位置使用捕獲組a在第一個捕獲組之前使用正斜杠。

"CallbackFnCreate_\/\*{3}([^"]+)(?=")

說明

  • "CallbackFnCreate_字面意思匹配
  • \\/\\*{3}匹配/和3次*
  • (捕獲組1
    • [^"]+匹配1次以上不是"
  • )關閉捕獲組
  • (?=")斷言右邊的是"

正則表達式演示

 const regex = /"CallbackFnCreate_\\/\\*{3}([^"\\n]+)(?=")/; const str = `1|#||4|3952|updatePanel|WorkflowFileExplorer_ajaxPanel|<div \\`id="WorkflowFileExplorer_pbContainer" style="width:0px;height:0px;overflow:hidden;position:relative;"><input type="button" name="WorkflowFileExplorer\\$pb" value="pb" onclick="javascript:__doPostBack(&#39;WorkflowFileExplorer\\$pb&#39;,&#39;&#39;)" id="WorkflowFileExplorer_pb" tabindex="-1" /><input name="WorkflowFileExplorer\\$postbackArgument" type="hidden" id="WorkflowFileExplorer_postbackArgument" value="CallbackFnCreate_/***TestResult_20190604-120620" /></div><div id="WorkflowFileExplorer_tree" class="RadTreeView RadTreeView_Default">\\``; let res = str.match(regex); console.log("/" + res[1]); 

 const unformattedName = 'CallbackFnCreate_/***TestResult_20190604-120620"'; const found = unformattedName.match(/CallbackFnCreate_(\\/)[\\W]{3}(.*?[^\\w-])/); document.write(found[1]+found[2]); 
第一個捕獲組是(/)這將匹配“/”,然后第二個捕獲組(。*?[^ \\ w-])將匹配“TestResult_20190604-120620”。

下面有更多解釋......

1)(/)\\是反斜杠並用於轉義“/”,因為/是正則表達式中的特殊字符,它告訴javascript這只是一個字符串而不是特殊字符。 因此“/”匹配。

2)[\\ W] {3} ---這不是一個組,因為它不在括號中。 [\\ W]表示捕獲任何非單詞[\\ W] {3}表示捕獲三個連續的非單詞,在您的情況下為***。

3)(。 ?[^ \\ w - ])-----。 意味着捕獲除換行符之外的任何數量的字符。 從字面上看,它意味着捕捉所有角色,但“?” 讓它變得懶惰。 因此意味着它應該捕獲所有字符,但應盡量保持最低限度。 [^ \\ w-]意味着它應該捕獲任何不是單詞的字符,即[a-zA-Z0-9_],也不應該捕獲“ - ”。 因此。 ?[^ \\ w - ] - 表示捕獲所有字符但在第一個非字字符后停止 - 除了 - ; 這是第2組。

4)組1 - 將輸出= /組2 - 將輸出='TestResult_20190604-120620'';

暫無
暫無

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

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