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

162
Visualizações
How to read each and every time a string is matched within a string

I have a very simple question. Lets say you have this string a and a string called b that is inside of a:

var b="anotherthing";
var a="something"+b+"\nsomething"+b;

How to find each line that contains b? Is it something like this:

var b="anotherthing";
var a="something"+b+"\nsomething"+b;
for(var i=a.search(b)-1 ; i<a.lastIndexOf(b,a.length) ; i=a.indexOf(b,i+1)){
     ///do something with `i`
}

How does one go for doing something like this as the code shown above DOES NOT work?

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

0

the concept of line is someting that is not defined. if you have a div with width 400px a test text will be 4 lines and if you have a div of 900px your text will be 2 lines.

first you need to define your line, like every 200 charecters or every 20 words, then you cut your string per 200 chareter and put each inside an array.

then you have write a for loop for your array of line strings. if each contains your target string, you can know and do something with that line

about 4 years ago · Juan Pablo Isaza Relatório

0

Break down the text into lines (split by \n) then count the occurrence for each line using regex.

const count = (mainString, substring) => (mainString.match(new RegExp(substring, 'g')) || []).length;

const statement = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, 
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset 
sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like 
Aldus PageMaker including versions of Lorem Ipsum.`

const needle = 'type'

const lines = statement.split('\n')

const occurrences = lines.reduce((accumulator, line, lineNumber) => {
  accumulator[`line ${lineNumber}`] = count(line, needle)

  return accumulator
} , {})

console.log(occurrences)

/*
{
  "line 0": 1,
  "line 1": 0,
  "line 2": 2,
  "line 3": 1,
  "line 4": 0,
  "line 5": 0,
  "line 6": 0
}
*/


about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming every line is separated by \n you could use split() to split a string into it's lines. Then you could use includes() for every line and check whether that lines contains "something".

If you want to return e.g. all the line numbers that contain "something" you could use reduce(). You could of course also return other stuff like the number of lines that contain a string or the lines themselves that contain the string.

const b = "something";
const a =`this is a mulitline string
where not ever line
contains "something"
but some linese do contain "something".
The lines that do contain
"something" are lines 3, 4 and 6.`;

const allLines = a.split("\n");
// contains all lines
console.log(allLines);
const linesWithSomething = allLines.reduce((linesWithSomething, currentLine, currentLineIndex) => {
  if(currentLine.includes(b)) linesWithSomething.push(currentLineIndex + 1);
  return linesWithSomething;
}, [])

console.log(`Lines with "${b}" are:`, linesWithSomething)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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