Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
match pattern nth digit of a number using regex

I am using a regex /^(\+88)?01[3-9]\d{8}$/

let phoneNum = "+880136486336"

let validRegex = /^(\+88)?01[3-9]\d{8}$/;

console.log(validRegex.test(phoneNum));

to match a pattern that matches some strings like

+8801712345678
01712345678
01312349876

It works fine but I also want to match 01[n] where n will be 3-9. pattern to find out the mobile network operator.

I want to do something like this

if the number is 01712345678 then print "Network Operator A",

if the number is +8801712345678 then print "Network Operator A"

if the number is 01312349678 then print "Network Operator B"

and different operators for different values of n.

here 017, 013, 014, 016, 015, 019, 018 are mobile operator codes which I want to use to find out operator based on the mobile number.

I don't find any way to do it. How can I do it with regex?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can capture the three digits at the start with a capturing group, then use String#match or RegExp#exec to get the actual match and then, if there is a match, get the group value and obtain the operator name from a dictionary:

const phones = ['+8801712345678','01712345678','01312349876'];
const operators = {'017' : 'Operator A', '013' : 'Operator C', '014' : 'Operator B', '016' : 'Operator D', '015' : 'Operator E', '019' : 'Operator F', '018' : 'Operator G'};
const validRegex = /^(?:\+88)?(01[3-9])\d{8}$/; 
for (const phone of phones) {
  const match = validRegex.exec(phone);
  if (match) {
    console.log(phone, "=> Valid, belongs to", operators[match[1]])
  } else {
    console.log(phone,"=> NO MATCH");
  }
}

Here,

  • ^(?:\+88)?(01[3-9])\d{8}$: the first optional group is now non-capturing and there is a capturing group with ID = 1 now, (01[3-9])
  • validRegex.exec(phone) gets the match object
  • operators[match[1]] returns the Operator name by the key.
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda