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

338
Visualizações
Regex JS- group default value / create an empty group if value not found

I have the following string:

"Group: I_am_group |SubGroup: I_am_subgroup"

And the following regex:

Group:\s(?<group>\w+) \|SubGroup:\s(?<subGroup>\w*)

The result is:

result

Until here- it's perfect to me, but when I am having the following case:

for the following strings:

1. "Group: I_am_group |SubGroup:"
2. "Group: I_am_group |"
3. "Group: I_am_group"

I want to get the result (e.g. for the string "Group: I_am_group"):

Match1      Group: I_am_group
group       I_am_group
subGroup    null

Is it possible with regex to create an empty group when it's not found? or give a default value to a group if the value not found?

In this case, I want to create the group named- 'subGroup' on the result with the value- null.

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

0

Unmatched groups per spec have value undefined, not null. A named group is unmatched, for example, when it appears inside another optional group that is unmatched.

Now take your three example strings that should produce an empty group and compare them with the original string "Group: I_am_group |SubGroup: I_am_subgroup".

  • In string (1.), " I_am_subgroup" is missing. That's \s(?<subGroup>\w*) in the regular expression.
  • In string (2.), "SubGroup: I_am_subgroup" is missing. That's SubGroup:\s(?<subGroup>\w*) in the regular expression.
  • In string (3.), " |SubGroup: I_am_subgroup" is missing. That's \|SubGroup:\s(?<subGroup>\w*) in the regular expression.

By enclosing all three of these parts in an optional group, we get

/Group:\s(?<group>\w+)( \|(SubGroup:(\s(?<subGroup>\w*))?)?)?/

This is a regular expression that matches in all cases with an empty match for subGroup. Note though that \w* can be replaced with \w+, unless you would like subGroup to be an empty string (not undefined) in cases when all other parts are present.

const regExp = /Group:\s(?<group>\w+)( \|(SubGroup:(\s(?<subGroup>\w+))?)?)?/;

const str1 = "Group: I_am_group |SubGroup:";
const str2 = "Group: I_am_group |";
const str3 = "Group: I_am_group";

console.log(str1.match(regExp).groups);
console.log(str2.match(regExp).groups);
console.log(str3.match(regExp).groups);

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