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

179
Vistas
How can i check for only one occurence only of each provided symbol?

I have a provided array of symbols, which can be different. For instance, like this - ['@']. One occurrence of each symbol is a mandatory. But in a string there can be only one of each provided sign.

Now I do like this:

const regex = new RegExp(`^\\w+[${validatedSymbols.join()}]\\w+$`);

But it also returns an error on signs like '=' and so on. For example:

/^\w+[@]\w+$/.test('string@=string')  // false

So, the result I expect:

  • 'string@string' - ok
  • 'string@@string - not ok
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Using a complex regex is most likely not the best solution. I think you would be better of creating a validation function.

In this function you can find all occurrence of the provided symbols in string. Then return false if no occurrences are found, or if the list of occurrences contains duplicate entries.

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
const escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

function validate(string, symbols) {
  if (symbols.length == 0) {
    throw new Error("at least one symbol must be provided in the symbols array");
  }

  const symbolRegex = new RegExp(symbols.map(escapeRegExp).join("|"), "g");
  const symbolsInString = string.match(symbolRegex); // <- null if no match
  // string must at least contain 1 occurrence of any symbol
  if (!symbolsInString) return false;

  // symbols may only occur once
  const hasDuplicateSymbols = symbolsInString.length != new Set(symbolsInString).size;
  return !hasDuplicateSymbols;
}


const validatedSymbols = ["@", "="];
const strings = [
  "string!*string", // invalid (doesn't have "@" nor "=")
  "string@!string", // valid
  "string@=string", // valid
  "string@@string", // invalid (max 1 occurance per symbol)
];

console.log("validatedSymbols", "=", JSON.stringify(validatedSymbols));
for (const string of strings) {
  const isValid = validate(string, validatedSymbols);
  console.log(JSON.stringify(string), "//=>", isValid);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you are looking for the following:

const regex = new RegExp(`^\\w+[${validatedSymbols.join()}]?\\w+$`);

The question mark means 1 or 0 of the previous group.

You might also need to escape the symbols in validatedSymbols as some symbols have a different meaning in regex

Edit:

For mandatory symbols it would be easier to add a group per symbol:

^\w+(@\w*){1}(#\w*){1}\w+$

Where the group is:

(@\w*){1}
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