web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Mask code

(0) ShareShare
ReportReport
Posted on by 250

Hello everyone my name is Taniguchi,  the id document you use in your country in mine is named "CPF" and i have a code to put a mask in the ID but i have trouble to understand some parts.

function maskCPF(executionContext) {

var attribute = executionContext.getEventSource();

var cpf = attribute.getValue();

cpf = cpf.toString();

 

cpf=cpf.replace(/\D/g,"");

cpf=cpf.replace(/(\d{3})(\d)/,"$1.$2");

cpf=cpf.replace(/(\d{3})(\d)/,"$1.$2");

cpf=cpf.replace(/(\d{3})(\d{1,2})$/,"$1-$2");

Xrm.Page.getAttribute("crd4c_cpf").setValue(cpf);

}

what the meaning of this parts:

cpf=cpf.replace(/(\d{3})(\d)/,"$1.$2");

cpf=cpf.replace(/(\d{3})(\d)/,"$1.$2");

cpf=cpf.replace(/(\d{3})(\d{1,2})$/,"$1-$2");

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi Taniguchi, 

    It simply means that it will replace the match of the regular expression, (\d{3})(\d), with the first capture group followed by a "." and then the second capture group. 

    So, if cpf was initially set to 12345, then after the line executes, 

    cpf=cpf.replace(/(\d{3})(\d)/,"$1.$2");

    it will be 123.45, i.e., place a after the first three digits. However, if you have cpf initially set to abc12345, then after the first line executes, it will be equal to abc123.45

     


    If you want to trace how regular expressions are evaluated, kindly look here (https://regex101.com/r/VwBrmU/1). That tells you the first capturing group is \d{3} which means, it is looking for a digit between [0-9] and the quantifier is 3, so it is looking exactly for 3 digits. 

     

     

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Taniguchi,

    As mentioned above as well, this is code to replace the current text based on the regular expression.

    Hope this helps.

  • Community Member Profile Picture
    on at

    @Ravi: Sorry for nitpicking, but it is not technically replacing the current text. It is simply reformatting based on the matched results of the regular expression criteria. It is a minor point, but a very important point to note. The use of $n doesn't replace even though it is used in conjunction with replace method. In fact, even the replace method returns a new string and the original string is left untouched.

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    yes technically you are right with the info around Replace method but wouldn't the below statement/code replace the current text of the variable cpf once executed?

    cpf=cpf.replace(/(\d{3})(\d)/,"$1.$2");

  • Community Member Profile Picture
    on at

    Yes, which is why I mentioned nitpicking.

    The OP's (Original Poster's) code will replace the text in variable cpf, but that is because of the assignment back to the cpf variable itself, not because of the replace function. Something similar to strings in C#, I can say 

    string myName = "Sara W Ellis";
    myName = myName.Replace(' ','.');
    Console.WriteLine(myName);


    And the output would be

    Sara.W.Ellis

    but it is not replacing myName with Sara.W.Ellis, it is returning a new string (original string is untouched) and I am just pointing the variable myName to the new string.  

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans