Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
Regex: How to match all uppercase characters of non-ascii string

I have strings like Japan Company, Chinese Company, and this regex /([A-Z])/g to get all uppercase characters then joining the result to make an emission of them. But when the input string is not in English letters then the regex does not work.

let string = "Japan Company";
console.log(string.match(/[A-Z]/g).join('')); // JC

But when I have a string like 日本の会社

let string = "日本の会社";
console.log(string.match(/[A-Z]/g).join(''));

This throws an exception as the result of the string.match(/[A-Z]/g) is null.

As I am trying to make elision to these strings and hieroglyphs do not have uppercases, the regex should match only first characters of each word where words are separated by spaces.

What generic regex should I use for this?

Something like POSIX's [:upper:] but this does not work for JavaScript regex engine.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use

(string.match(/(?<!\S)\S/g) || [string]).join('')

See the JavaScript demo:

const strings = ["Japan Company", "japan company", "日本の会社"];
for (const string of strings) {
    console.log(string, '=>', (string.match(/(?<!\S)\S/g) || [string]).join('').toUpperCase())
}

The (?<!\S)\S regex matches a non-whitespace char at the start of string or after a whitespace char.

A Safari, non-lookbehind, pattern:

var strings = ["Japan Company", "japan company", "日本の会社"];
for (var i=0; i<strings.length; i++) {
    var m = strings[i].match(/(?:^|\s)(\S)/g)
    if (m === null) {
        console.log(strings[i], '=> ', strings[i])
    } else {
        console.log(strings[i], '=>', m.join('').replace(/\s+/g, '').toUpperCase())
    }
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Based on your comments, I think this should do what you want.

function getUppercaseLetters(string) {
    // Find uppercase letters in the string
    let matches = string.match(/[A-Z]/g);
    // If there are no uppercase letters, get the first letter of each word
    if (!matches) matches = string.split(" ").map(word => word[0]);
    // If there are still no matches, return an empty string. This should prevent against any edge cases.
    if (!matches) return "";
    // Join the array elements and make it uppercase.
    return matches.join("").toUpperCase();
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda