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

125
Visualizações
Regex to match a string only inside another string

Suppose here is a sample text:

Hello this is testing _testisgood _test test ilovetesting again test

The regex

/test/gi

Gives all the test but I only want the test string which is surrounded by some other character except space means the opposite of exact match. In other words the test in testing , _testisgood ,ilovetesting i want to match.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Bill's answer is good but may you like this one: just find all words with test and then filter out useless ones;

const s = "Hello this is testing _testisgood _test test ilovetesting again test"
 
console.log(
    (s.match(/[^\s]*test[^\s]*/gi) || []).filter(s => s !== 'test')
)
about 4 years ago · Santiago Trujillo Relatório

0

The regex below will match 'test' when it either has a non-whitespace character(s) prefixing or post fixing it.

/([^\s]+test[^\s]*|[^\s]*test[^\s]+)/gi;

OR

/(\S+test\S*|\S*test\S+)/gi;

const sentence = "Hello this is testing _testisgood _test test ilovetesting again test";

regex = /([^\s]+test[^\s]*|[^\s]*test[^\s]+)/gi;

console.log(sentence.match(regex));

about 4 years ago · Santiago Trujillo Relatório

0

You can match just _testisgood and ilovetesting in your example by specifying one or more characters that are not whitespace before and after test, like this:

/[^\s]+test[^\s]+/gi

If you also want to match testing, then drop [^\s]+ from the beginning of the pattern.

about 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