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

269
Visualizações
Regex String.split(/\s*/) gives different results in JavaScript and C#

I have a Regex in JS which does not produce same results in C#:

//javascript
var password = "pass";
var arrPws = password.split(/\s*/);

gives me a String[] result like the following


But when I try to reproduce it in C# I get a slightly different output:
//c#
using System.Text.RegularExpressions;

var password = "pass";
var arrPwd = Regex.Split(password,@"\s*");

the C# code adds an extra "" in the beginning and end like following
How do I eliminate the extra "" in C#?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

For a quick fix to the .NET code, you may split on (?<=.)\s*(?=.):

using System.Text.RegularExpressions;

var password = "pass";
var arrPwd = Regex.Split(password,@"(?<=.)\s*(?=.)");
foreach(var item in arrPwd)
{
    Console.WriteLine(item.ToString());
}

This prints:

p
a
s
s

The issue you are seeing has to do with the behavior of \s*. On .NET's regex engine, \s* matches the zero width markers at the very start and end of the input, but not in JavaScript's engine.

If you just want an array of all characters in the string, use string#ToCharArray().

over 4 years ago · Santiago Trujillo 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