Regular Expression for RG

Asked

Viewed 10,462 times

8

I have a expressão regular which defines the points according to the maximum number of digits.

Expression:

function Rg(v){
    v=v.replace(/\D/g,"");
    if(v.length == 9) v=v.replace(/(\d{2})(\d{3})(\d{3})(\d{1})$/,"$1.$2.$3-$4");
    return v
}

RG : 88.888.888-8

But I would like an expression that Padrones with different digits. Example:

RG : 8.888.888-8

Without the use of another if as the following example:

function Rg(v){
    v=v.replace(/\D/g,"");
    if(v.length == 9) v=v.replace(/(\d{2})(\d{3})(\d{3})(\d{1})$/,"$1.$2.$3-$4");
    if(v.length == 8) v=v.replace(/(\d{1})(\d{3})(\d{3})(\d{1})$/,"$1.$2.$3-$4"); 
    return v
}

It is possible?

  • this rule does not apply to all states. In Minas Gerais have Rgs with the format MG-14.808.688

2 answers

14


Friend, here you are, don’t forget to consider who helps you instead of asking other questions and forgetting the answers. Help the whole community! ;)

function Rg(v){
    v=v.replace(/\D/g,""); //Substituí o que não é dígito por "", /g é [Global][1]
    v=v.replace(/(\d{1,2})(\d{3})(\d{3})(\d{1})$/,"$1.$2.$3-$4"); 
    // \d{1,2} = Separa 1 grupo de 1 ou 2 carac. (\d{3}) = Separa 1 grupo de 3 carac. (\d{1}) = Separa o grupo de 1 carac.
    // "$1.$2.$3-$4" = recupera os grupos e adiciona "." após cada.

        return v
    }

Role-modelMSDN Link:

function styleHyphenFormat(propertyName)
{
  function upperToHyphenLower(match)
  {
    return '-' + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
  • It was a mistake at the time, because I had already done the one with the if, so I ended up deleting to change the question!

  • Bah man, I wanted Regex food like this! Good Rony! D

  • Could you explain what exactly every part of your expression does.

  • @Ronny Amarante out of curiosity of college, se caso o RG were 888.888-8 the expression would be like?

  • @Kazzkiq, I added some explanations, I hope I can express well what each thing does.

  • @Felipe: the ideal would be for you to pass a "$1. $2 etc." function, because it will always return "*. *000.000-0" using this example... With function you can format better. I will leave an example with function.

  • @Ronny Amarante Thanks for the help, I got to do what I wanted!

  • 1

    This regex admits that the RG checker digit can only be one number, and actually can be X as well. It needs to change \d{1} for [\dX].

Show 3 more comments

1

MORE CORRECT

It was almost good in the @Ronnyamarante reply, but as comments, forgot the "X"... As it is not always possible to deal with errors, here includes the parameter errChar just to remind you or not with a "?" that there’s something off about the ID.

function RgFormat(v0,errChar='?'){
    var v = v0.toUpperCase().replace(/[^\dX]/g,'');
    return (v.length==8 || v.length==9)?
       v.replace(/^(\d{1,2})(\d{3})(\d{3})([\dX])$/,'$1.$2.$3-$4'):
       (errChar+v0)
    ;
} 

NOTE: as it accepts start with 1 or 2 digits (it could also fill with zero), it is important to ^ for completeness in the case of 2 digits.

... Since we speak in error message, it remains to validate the check digit ($4), vide question Validation of RG or this didactic explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.