JavaScript Regular expression match substitution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.