简体   繁体   中英

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|#||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">`

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:

(.+?)

Test

 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); 

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(&#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) 

You could use a capturing group an in the replacement place a forward slash between before the first capturing group.

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

Explanation

  • "CallbackFnCreate_ Match literally
  • \\/\\*{3} match / and 3 times *
  • ( Capture group 1
    • [^"]+ Match 1+ times not a "
  • ) Close capturing group
  • (?=") Assert what is on the right is a "

Regex demo

 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]); 
The first capturing group is (/) this will match the "/" then the second capturing group which is (.*?[^\\w-]) will match the "TestResult_20190604-120620".

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. Therefore the "/" is matched.

2) [\\W]{3}--- this is not a group since it is not in brackets. [\\W] means capture any non-word [\\W]{3} means capture three consecutive non words which in your case is ***.

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. therefore . ?[^\\w-]-- means capture all characters but stop after the first non word character except -; this is group 2.

4) group 1 -- will output = / group 2 -- will output = 'TestResult_20190604-120620"';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM