[英]Regex to append character and remove end double quotes
I have tried below regex to capture some string out of it : 我试过下面的正则表达式来捕获它的一些字符串:
"CallbackFnCreate_[\W]{4}(.*?[^\w-])
Required Output should be : /TestResult_20190604-120620 (Capture
TestResult_20190604-120620 and append "/" in the beginning)
My Group 1 Output is : TestResult_20190604-120620" 我的第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('WorkflowFileExplorer$pb','')" 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">`
My guess is that we wish to simply do a replacement and slightly modify the original expression: 我的猜测是我们希望简单地做一个替换并略微修改原始表达式:
.+("CallbackFnCreate_\/\*\*\*(.+?)").+
which our desired output is here in this capturing group: 我们期望的输出在这个捕获组中:
(.+?)
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('WorkflowFileExplorer\\$pb','')" 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);
Just use parentheses to capture the relevant part and append the "/" at replacement. 只需使用括号捕获相关部分并在替换时附加“/”。
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('WorkflowFileExplorer$pb','')" 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)
You could use a capturing group an in the replacement place a forward slash between before the first capturing group. 您可以在替换位置使用捕获组a在第一个捕获组之前使用正斜杠。
"CallbackFnCreate_\/\*{3}([^"]+)(?=")
Explanation 说明
"CallbackFnCreate_
Match literally "CallbackFnCreate_
字面意思匹配 \\/\\*{3}
match /
and 3 times *
\\/\\*{3}
匹配/
和3次*
(
Capture group 1 (
捕获组1
[^"]+
Match 1+ times not a "
[^"]+
匹配1次以上不是"
)
Close capturing group )
关闭捕获组 (?=")
Assert what is on the right is a "
(?=")
断言右边的是"
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('WorkflowFileExplorer\\$pb','')" 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]);
More explanation below.... 下面有更多解释......
1) the (/) \\ is backslash and is used to escape the "/" since / is a special character in regular expressions it tells javascript that this is just a string and not a special character. 1)(/)\\是反斜杠并用于转义“/”,因为/是正则表达式中的特殊字符,它告诉javascript这只是一个字符串而不是特殊字符。 Therefore the "/" is matched. 因此“/”匹配。
2) [\\W]{3}--- this is not a group since it is not in brackets. 2)[\\ W] {3} ---这不是一个组,因为它不在括号中。 [\\W] means capture any non-word [\\W]{3} means capture three consecutive non words which in your case is ***. [\\ W]表示捕获任何非单词[\\ W] {3}表示捕获三个连续的非单词,在您的情况下为***。
3) (. ?[^\\w-])----- . 3)(。 ?[^ \\ w - ])-----。 means capture any amount of characters except newline. 意味着捕获除换行符之外的任何数量的字符。 Literally it is meant to capture all characters but the "?" 从字面上看,它意味着捕捉所有角色,但“?” makes it lazy . 让它变得懒惰。 ? ? therefore means that it should capture all characters but should try to keep it at minimum. 因此意味着它应该捕获所有字符,但应尽量保持最低限度。 [^\\w-] means it should capture any character that is not a word ie [a-zA-Z0-9_] and should not capture"-" too. [^ \\ w-]意味着它应该捕获任何不是单词的字符,即[a-zA-Z0-9_],也不应该捕获“ - ”。 therefore . 因此。 ?[^\\w-]-- means capture all characters but stop after the first non word character except -; ?[^ \\ w - ] - 表示捕获所有字符但在第一个非字字符后停止 - 除了 - ; this is group 2. 这是第2组。
4) group 1 -- will output = / group 2 -- will output = 'TestResult_20190604-120620"'; 4)组1 - 将输出= /组2 - 将输出='TestResult_20190604-120620'';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.