averigüe si la cadena comienza con "vybe" seguida de números en javascript [cerrado]

I want to know how to find out if a string starts with "vybe" followed by numbers..like this : "vybe1232", in javascript. Would this be done using regex?

preguntado el 10 de marzo de 12 a las 00:03

yes. something like /^vybe[0-9]{4}/ -

3 Respuestas

var str = "vybe1234";
var re = /^vybe\d+$/
console.log( re.test(str) );
  • ^ start of a string
  • vybe match the characters
  • \d+ match one or more digits
  • $ fin de cuerda

respondido 10 mar '12, 00:03

I'd remove the end-of-string, op didn't actually specify that. Good answer though. - Madbreaks

Yes, you can use a regexp, with String.match():

if (myString.match(/^vybe\d+/)) {
    // it matches!
}

Your question was slightly ambiguous about the fin of the string - if you want it to , solamente contain the prefix and digits, put a $ antes de la final / personaje:

if (myString.match(/^vybe\d+$/)) {
    // it matches!
}

respondido 10 mar '12, 00:03

Use una expresión regular simple:

 var str1 = 'vybe1234', 
     str2='other111', 
     re=/^vybe[0-9]+/;

 alert( str1.match(re) ); // shows "vybe1234" match
 alert( str2.match(re) ); // shows "null" no match

respondido 10 mar '12, 00:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.