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

81
Vistas
Javascript obtiene la palabra completa después de hacer coincidir el carácter especial en la cadena

Este código no se ejecuta cuando se pone Textx-yz pero funciona bien cuando se pone Textx-y

Quiero una alerta (Textx-yz) cuando pongo Textx-,Textx,Textx-yz en coincidencia

 alert('Textabc Textx-yz TextTypeTestingText'.match(/Textx-yz\S+/)[0])
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

puede usar límites de palabras y coincidencias exactas para obtener esta coincidencia:

 \b(Textx\-yz)+\b

\b - busca solo palabras completas

y en el medio le decimos lo que queremos encontrar

about 4 years ago · Juan Pablo Isaza Denunciar

0

Si entiendo lo que quieres decir, puedes usar este patrón:

 'Textabc Textx-yz TextTypeTestingText'.match(/Textx-yz \S+/)[0]

En realidad, agrego un espacio después de -yz en el método de coincidencia

about 4 years ago · Juan Pablo Isaza Denunciar

0

Si necesita que la expresión coincida en cualquier cadena dada con el patrón Textx seguido o no por un guión y [a-ZA-Z0-9_] caracteres, la expresión regular que está buscando es: (Textx(-[\w]+)?)

No está perfectamente claro qué esperaba cuando la cadena contenía una expresión no válida como 'Textx-no deseado'. En este caso, mi código devuelve 'Textx' sin guión siguiente. De todos modos, separé el grupo de captura para la segunda parte para que pueda agregar más lógica en la función doMatch haciendo algunas consideraciones más allá de la expresión regular.

Aquí hay una demostración:

 function doMatch(subject){ //this is an expression having 1 optional grouped capture as being the part "dash following something" let pattern = /Textx(-[\w]+)?/; let match = subject.match(pattern); //the whole text matched by the expression //console.log(match[0]); //the optional text matched after Textx starting with dash and at least one \w char //this will be undefined if 'Textx-' or 'Textx' but not for 'Textx-anything' //console.log(match[1]); return match[0]; } console.log('------begin------'); let matchedString; //1- Textx (alone) matchedString = doMatch('Textx'); console.log( matchedString ); //-> Textx //2- Textx (alone and having further text later separated by space) matchedString = doMatch('Textabc Textx unwanted'); console.log( matchedString ); //-> Textx //3- Textx (followed by '-randomstuff') matchedString = doMatch('Textabc Textx-randomstuff'); console.log( matchedString ); //-> Textx-randomstuff //4- Textx (followed by '-randomstuff' and having further text later separated by space) matchedString = doMatch('Textabc Textx-randomstuff unwanted'); console.log( matchedString ); //-> Textx-randomstuff //5- Textx- (where the dash is not followed by valid characters and it's the end of string) matchedString = doMatch('Textx-'); console.log( matchedString ); //-> Textx //6- Textx- (where the dash is not followed by valid characters and there's further text) matchedString = doMatch('Textabc Textx- unwanted'); console.log( matchedString ); //-> Textx

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