Home > Enterprise >  How to read each and every time a string is matched within a string
How to read each and every time a string is matched within a string

Time:05-08

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?

CodePudding user response:

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

CodePudding user response:

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; }

CodePudding user response:

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
}
*/


  • Related