0

I have a big block of text which I've split by new line, so each item in the array is a line of text.

I'm looping through these lines and trying to detect where a line includes a </mark> but doesn't include a <mark>, and if this condition has been met then it removes the </mark> (as it's missing an opening tag).

final_formatted_log_split = logtext.split("\n");

for (i = 0, l = final_formatted_log_split.length; i < l; i++) {
  if (final_formatted_log_split[i].includes("<mark>") === false) {
    if (final_formatted_log_split[i].includes("</mark>") === true) {
      var removed_mark = final_formatted_log_split[i].replace("</mark>", "");
    }
  }
}

var final_formatted_log = final_formatted_log_split.join("\n");
console.log(final_formatted_log);

and this console log still includes in the text where it doesn't include a

Just to be super clear, the expected outcome is the following:

if a line is like this:

line of text here</mark>

then it needs to remove the </mark> because it does not contain an opening <mark>

I suspect it is to do with the === false, but from what I've read online that is how others have used .includes to see if something does 'not include'

4
  • Can there be multiple <mark>s or nested ones on the same line? Commented Oct 17, 2019 at 8:26
  • @CertainPerformance althought the replace function should've removed duplicates as well... Commented Oct 17, 2019 at 8:29
  • OP- Can you give us an example for a text line for which your code does not work? what was before and what comes out after? Commented Oct 17, 2019 at 8:30
  • @Gibor unfortunately no, the replace function only replaces the first occurrence, if you don't specify a RegExp with g flag - see the second variation in the first demo: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… or on the same page under: substr (pattern) Commented Oct 17, 2019 at 8:46

2 Answers 2

2

You can do it with simple String.prototype.includes:

const arr = [
  '<mark>1</mark>',
  '2</mark>',
  '3</mark></mark>',
  '<mark>4</mark>',
]

const replaceMark = (arr) => {
  return arr.map(e => {
    if (e.includes('</mark>') && !e.includes('<mark>')) e = e.replace(/\<\/mark\>/g, '')
    return e
  })
}

console.log('original:', arr)
console.log('replaced:', replaceMark(arr))

This solution doesn't handle complex situations like <mark>text</mark></mark>, only the most basic ones.

Sign up to request clarification or add additional context in comments.

Comments

1

There's nothing wrong with ===false.Its working properly.To check this one you just put a console.log inside if block.

What you are doing here is,you are not replacing array value with modified one.So replace this line

var removed_mark = final_formatted_log_split[i].replace("</mark>", "");

with

final_formatted_log_split[i] = final_formatted_log_split[i].replace("</mark>", "");

And you can use one if block instead of two if block.

var final_formatted_log_split = logtext.split("\n");;

for (i = 0, l = final_formatted_log_split.length; i < l; i++) {
  if (!final_formatted_log_split[i].includes("<mark>") && final_formatted_log_split[i].includes("</mark>")) {
      final_formatted_log_split[i] = final_formatted_log_split[i].replace("</mark>", "");
  }
}

var final_formatted_log = final_formatted_log_split.join("\n");
console.log(final_formatted_log);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.