Javascript - How To Use Regex Process The Following Complicated String
Solution 1:
You need to put each component within an alternation in a grouping construct with maximum match try of 3
if it is necessary:
\[SM_g]word(\[SM_h])((?:\.|\[SM_l]| ?"){0,3})
You may replace word
with .*?
if it is not a constant or specific keyword.
Then in replacement string you should do:
$1$3$2
var re = /(\[SM_g]word)(\[SM_h])((?:\.|\[SM_l]| ?"){0,3})/g;
var str = `[SM_g]word[SM_h][SM_l] ".`;
console.log(str.replace(re, `$1$3$2`));
Solution 2:
This seems applicable for your process, in other word, changing sub-string position
.
(\[SM_g])([^[]*)(\[SM_h])((?=([,\.])|(\[SM_l])|( ?&\\?quot;)).*)?
Demo,,, in which all sub-strings are captured to each capture group
respectively for your post processing
.
[SM_g]
is captured to group1
, word
to group2
, [SM_h]
to group3
, and string of all trailing part
is to group4
, [,\.]
to group5
, [SM_l]
to group6
, " ?&\\?quot;
" to group7
.
Thus, group1~3
are core part
, group4
is trailing part
for checking if trailing part exists, and group5~7
are sub-parts of group4
for your post processing.
Therefore, you can get easily matched string's position changed output string
in the order of what you want by replacing with captured groups
like follows.
\1\2\7\3or $1$2$7$3 etc..
For replacing in Javascript, please refer to this post. JS Regex, how to replace the captured groups only?
But above regex is not sufficiently precise because it may allow any repeatitions of the sub-part
of the trailing string, for example, \1\2\3\5\5\5\5
or \1\2\3\6\7\7\7\7\5\5\5
, etc..
To avoid this situation, it needs to adopt condition which accepts only the possible combinations
of the sub-parts of the trailing string. Please refer to this example. https://regex101.com/r/6aM4Pv/1/ for the possible combinations
in the order.
But if the regex adopts the condition of allowing only possible combinations
, the regex will be more complicated so I leave the above simplified regex to help you understand about it. Thank you:-)
Post a Comment for "Javascript - How To Use Regex Process The Following Complicated String"