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

101
Vistas
Sanitizing strings with regex, normalize strips entire japanese characters

I previously made a question on how to use regex to sanitize a string that will be used on an URL. The code I am using in my API is this:

const newPost = new Post({
    category: req.body.category,
    user: req.body.user,
    title: req.body.title,
    content: req.body.content,
    shadowBanned: req.body.shadowBanned,
    community: req.body.community,
    categoryMeta: req.body.categoryMeta,
    image: req.body.imageUrl,
    externalLink: req.body.link,
    urlHash: req.body.title
      .replace(/(\s|-)/g, '_')
      .normalize('NFKD')
      .replace(/\W/g, '')
      .toLowerCase(),
    postUrl:
      req.body.community +
      '/c/' +
      req.body.category +
      '/p/' +
      req.body.title
        .replace(/(\s|-)/g, '_')
        .normalize('NFKD')
        .replace(/\W/g, '')
        .toLowerCase(),
    source: req.body.link
      ? new URL(req.body.link).hostname.replace('www.', '')
      : '',
  });

The regex on urlHash and postUrl works well with western languages, it will output strings formatted like esp/c/CategoryName/ThisIsMyTitleWithoutSymbolsAndSpaces (title case pun intended for reading purposes) but when it comes to use Japanese or Korean characters, that title will be empty or left with some characters, here's an example:

const text = '「Apex Legends」次期大型アップデート“デファイアンス”のローンチトレイラーが公開に'
// will output apex_legends

If there's any other western character, the replace() code will just wipe everything.

Is there a way to avoid this or do I need to use a different condition if language is equal to any other non western language? I would love not using conditionals.

I don't think it's a problem to use Japanese characters in the URL, but symbols.

about 4 years ago · Juan Pablo Isaza
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