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

156
Visualizações
How to get exact pattern which has specific word by using Regex in javascript

I have a question for you. I write simple regex but it didn't work way that what I want.

My regex:

/^\bname\b="([^"]*)"$/

But it did not work. So I decided to change that regex to this:

/name="([^"]*)"$/

this is perfectly fine to me when I have just one match case. In other words, This is working fine this case:

let my_case = 'Content-Disposition: form-data; name="body"';
let result = my_case.match(/name="([^"]*)"$/);
console.log(result);

but when I change case string to like this:

let my_case = 'Content-Disposition: form-data; name="relatedFile"; filename="article_217605.pdf"';
let result = my_case.match(/name="([^"]*)"$/);
console.log(result);

Result be like:

[
  'name="article_217605.pdf"',
  'article_217605.pdf',
  index: 24,
  input: 'name="relatedFile"; filename="article_217605.pdf"',
  groups: undefined
]

I don't wanna this actually. I want to get (name="relatedFile") how to get this in this case? pls help me!

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

0

The pattern ^\bname\b="([^"]*)"$ does not match as it asserts the start and the end of the string. In a pattern like this, the word boundaries \b are also implicit and can be omitted from the pattern.

This pattern that you tried name="([^"]*)"$ will allow a partial match starting with name and then will only match as it can assert the end of the string and will give you the wrong match.

If you would start that pattern with a word boundary, there will still be no match because it will then not allow a partial match anymore.

You could start the match with a word boundary and then capture in group 1 the value between double quotes without using any of the other anchors.

\bname="([^"]*)"

See the match on regex101.

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Since it's a string
  • Instead of regex you can split and map element for this case

let somecase = 'Content-Disposition: form-data; name="relatedFile"; filename="article_217605.pdf"';

let res = somecase.split(";").map(ele => ele)[1];
console.log(res)

  • Using Regex

let somecase = 'Content-Disposition: form-data; name="relatedFile"; filename="article_217605.pdf"';

let res = somecase.split(";").find(ele => /name=.*/g.test(ele));
console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

I got exactly correct answer. By the way thank you to answering my question @The fourth bird.

His answer is:

/\bname="[^"]*"/

But it doesn't work well because result couldn't give me name value. So I change it little bit.

Correct perfect answer is:

/\bname="([^"]*)"/

thank you guys! I am really appreciate it.

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