replace the match with the match result of the regular expression with *
functionregreplace (Reg, str) {varResult//Final output ResultsOut//The matching results returned by the regular exec each time it is run. Index//matches the position in the stringLength//Match Lengthresult= Str.split ("");//separate the strings to be matched into groups, waiting to be processed while(out = reg.exec (str)) {//Returns NULL if the match succeeds in returning an out array if there is no match or reaches the end of the stringlength = Out[0].length;//Match Length for(vari = 0; i < length; i++) {//From the position at which the match was started, replace it with the match length loop as *index =Reg.lastindex; Result.splice (Index-length + I, 1, "*"); } } returnReg + "\ n" + str + "\ n" + result.join ("");//return Results}
Test One:
var strs = ' [email protected] '; var regs =/[\w\-][email protected][a-za-z\d] ((\-)? [ A-ZA-Z\D]) * (\.[ A-za-z\d] ((\-)? [ A-ZA-Z\D] *) * (\.[ a-za-z]{2,4})/g; var outcome = Regreplace (regs, STRs); Console.log (outcome);
Output:
/[\w\-][email protected][a-za-z\d] ((\-)? [ A-ZA-Z\D]) * (\.[ A-za-z\d] ((\-)? [ A-ZA-Z\D] *) * (\.[ a-za-z]{2,4})/g
[Email protected]
*****************
Test Two:
var str = ' a AA AAA AAAA ab ABB Abbba ABCBA '; var reg =/ab+/g;
Output: /ab+/* * * * ****a **CBA
JavaScript Regular expression match substitution