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

148
Vistas
First letter of each word in a string and punctuation

Input: Aliquam ipsum ex, tempus ornare semper ac, varius vitae nibh.

Output: A i e, t o s a, v v n.

I need a javascript function to solve this.

I'm trying something like this:

function short_verse(verse) {
  let result = [];

  verse.split(' ').map(word => word.charAt(0) != '' ? result.push(word.charAt(0)) : '');

  return result.join(" ");
}

let input = "Aliquam ipsum ex, tempus ornare semper ac, varius vitae nibh.",
  output = short_verse(input);

console.log(output);

The story: They say you can memorize texts this way. :) So, I create an application that will include this feature, too.

It should work for non-ascii chars, too. Example:

Input: Aliqușam țipsum ex, tempăs ornâre semper ac, varius vitae îbh.

Output: A ț e, t o s a, v v î

Note: In my case romanian diacritics would be enough - ăâîșțĂÂÎȘȚ.

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

0

We can use a regex replacement approach here:

var input = "Aliquam ipsum ex, tempus ornare semper ac, varius vitae nibh.";
var output = input.replace(/(\w)\w*/g, "$1");
console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you are using only word characters, you can keep the first character and remove the rest of the word characters.

\B matches a non word boundary and \w+ matches 1 or more word characters:

const s = "Aliquam ipsum ex, tempus ornare semper ac, varius vitae nibh.";
console.log(s.replace(/\B\w+/g, ""));

For the updated question, you can capture leading chars other than any letter or whitespace char, followed by a single letter. Follow optional letters that should be removed, and use capture group 1 in the replacement.

([^\p{L}\s]*\p{L})\p{L}*

See the regex matching in this regex demo.

[
  "Dumnezeu a zis: „Să fie o întindere între ape, și ea să despartă apele de ape.”",
  "Aliqușam țipsum ex, tempăs ornâre semper ac, varius vitae îbh.",
  "Aliquam ipsum ex, tempus ornare semper ac, varius vitae nibh."
].forEach(s =>
  console.log(s.replace(/([^\p{L}\s]*\p{L})\p{L}*/gu, "$1"))
)

about 4 years ago · Juan Pablo Isaza Denunciar

0

The following function should work for characters, numbers and symbols. The magic is in the regex; [a-zA-ZÀ-ÿăâîșțĂÂÎȘȚ]+ extracts all unique words that contain alphanumeric and romanian alphabet characters (as per question request), \s extracts all space characters as we want to preserve the spacing and finally ^\w\s extracts all non-alphanumeric and non-space characters - a.k.a symbols:

function short_verse(verse) {
  let result = [];
  const tokens = verse.match(/([a-zA-ZÀ-ÿăâîșțĂÂÎȘȚ]+)|(\s)|[^\w\s]/g);
  const firstChars = tokens.map((token) => token.charAt(0));
  return firstChars.join('');
}

let input1 = "Aliquam ipsum ex, tempus ornare semper ac, varius vitae nibh.";
console.log(short_verse(input1));
let input2 = "Să fie o întindere între ape, și ea să despartă apele de ape."
console.log(short_verse(input2));

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