Home > OS >  How to delete specified text in a string if it contains specific text and do it for each line in a t
How to delete specified text in a string if it contains specific text and do it for each line in a t

Time:08-05

I need to delete <annot:annotationLevel><annot:body> and </annot:body></annot:annotationLevel> only if the line contains "(a)" through "(zzz)"

It's for something that looks like this:

<annot:annotationLevel><annot:body><p>“(a) Definitions. In this section:</p></annot:body></annot:annotationLevel>
<annot:annotationLevel><annot:body><p>“(1) List of Material Interested Parties. The term “List of Material Interested Parties” means the List of Material Interested Parties established under subsection (c)(1).</p></annot:body></annot:annotationLevel>
<annot:annotationLevel><annot:body><p>“(2) Oversight Board. The term “Oversight Board” has the meaning given the term in section 5 of PROMESA (48 U.S.C. 2104).</p></annot:body></annot:annotationLevel>
<annot:annotationLevel><annot:body><p>“(b) Required Disclosure.</p></annot:body></annot:annotationLevel>

In the above sample, (a) and (b) should have their annot tags deleted and only have p tags left.

NOTE: It should only work for lowercase alpha characters and NOT on uppercase alpha. (Now I wonder how I'll differentiate alpha with roman numeral (i), (v), and (x), cause those shouldn't be affected, but maybe that's for later)

I've been solving this but my current solution isn't working for some reason. In this one, I'm only trying to target "(a)" and not yet the rest using RegEx.

function annotationWrapCleanups() {
  let lines = document.querySelector('#textarea3').value.split('\n');
  for (let i = 0; i < lines.length; i  ) {
    if (lines[i] !== "" && lines[i].includes('(a) Definitions.')) {
      lines[i].replace('<annot:annotationLevel><annot:body>', '')
      lines[i].replace('</annot:body></annot:annotationLevel>', '')
    }
  }
}
.main-textarea {
  height: 50px;
  width: 99%;
}

.main-textarea:focus {
  outline: none;
}

.cleanup-btn {
  padding: 10px;
  margin-top: 10px;
}
<textarea  id="textarea3"></textarea>
<button  onclick="annotationWrapCleanups();">clean</button>

Anyways, thank you in advance for any help, any insight would be appreciated!

CodePudding user response:

Check if the line matches the Regex /<p>“\([a-z] \)/.

Our matched string should:

  • <p>" Start with this. So that we're looking at the beginning of the element's text content.
    • This avoids matching things like (c) in your second line
  • \( Match an open bracket.
    • \ is to escape what ( normally does in regex.
  • [a-z] Match one or more lowercase letters
  • \) Match a closing bracket

Using combining this and roman numerals will be a whole different beast, because the simplest question would be, how could you know if it's (i) the letter or (i) as in one.

I set it up for you in the snippet, just click clean and check if the results match what you're looking for. I added the extra line breaks after cleaning to make it clearer to see.

function annotationWrapCleanups() {
  const textArea = document.querySelector('#textarea3')
  let lines = textArea.value.split('\n');
  for (let i = 0; i < lines.length; i  ) {
    if (lines[i].match(/<p>“\([a-z] \)/g)) {
      lines[i] = lines[i].replace('<annot:annotationLevel><annot:body>', '')
      lines[i] = lines[i].replace('</annot:body></annot:annotationLevel>', '')
    }
  }
  textArea.value = lines.join('\r\n\n')
}

document.querySelector('#textarea3').value = `<annot:annotationLevel><annot:body><p>“(a) Definitions. In this section:</p></annot:body></annot:annotationLevel>
<annot:annotationLevel><annot:body><p>“(1) List of Material Interested Parties. The term “List of Material Interested Parties” means the List of Material Interested Parties established under subsection (c)(1).</p></annot:body></annot:annotationLevel>
<annot:annotationLevel><annot:body><p>“(2) Oversight Board. The term “Oversight Board” has the meaning given the term in section 5 of PROMESA (48 U.S.C. 2104).</p></annot:body></annot:annotationLevel>
<annot:annotationLevel><annot:body><p>“(b) Required Disclosure.</p></annot:body></annot:annotationLevel>`
.main-textarea {
  height: 50px;
  width: 99%;
}

.main-textarea:focus {
  outline: none;
}

.cleanup-btn {
  padding: 10px;
  margin-top: 10px;
}
<textarea  id="textarea3"></textarea>
<button  onclick="annotationWrapCleanups();">clean</button>

  • Related