1

I have this text:

Blah Blah Blah SPECIAL TEXT: PART OF IT
blah bah SPECIAL TEXT: STILL PART OF IT
blah blah blah blah

How do I use JavaScript to get this result:

Blah Blah Blah
blah bah
blah blah blah blah

How do I remove the text in capital letters.

Note that the part after the ":" and before the line break is not meant to be in capital, but it's part of the text that has to go, so I can't just say remove all text in capital.

Also note that I do not know exactly where the text to be removed will start and end, what I do know is that it starts when there is an occurrence of several consecutive capital letters typed side by side and ends when there is a line break.

I've tried getting the index of the first few capital letters by just using search("SPECIAL"), and getting the end by using search("\n"). The issue with this is getting the line break after the "SPECIAL TEXT" and no other.

So finally, if I do get the positions, both starting and ending, I guess I could just get whatever string is in between the two indexes, and replace them with an empty string and iterate the process.

Any help will be appreciated.

4 Answers4

3

How about this regular expression?

("Blah Blah Blah SPECIAL TEXT: PART OF IT\n" +
"blah bah SPECIAL TEXT: STILL PART OF IT\n" +
"blah blah blah blah").replace(/SPECIAL\sTEXT:.+$/mg,'')

This basically means replace any string fragments starting with SPECIAL TEXT: followed by some characters (.+) and ending with end of line ($). Examine all lines individually instead of whole string at once (m) and don't stop after the first one replacement (g)

Mchl
  • 61,444
  • 9
  • 118
  • 120
2

Try a regex:

var str = "Blah Blah Blah SPECIAL TEXT: PART OF IT\nblah bah SPECIAL TEXT: STILL PART OF IT\nblah blah blah blah"
console.log(str.replace(/SPECIAL.*$/gm, ''))
/*
Blah Blah Blah
blah bah
blah blah blah blah
*/

SPECIAL: match “SPECIAL”
.*: match anything but \n (newline)
$: match the end of the line
/g: match multiple times
/m: match $ on the end of each line, not just the end of the string
'': replace with nothing.

Jed Fox
  • 2,979
  • 5
  • 28
  • 38
0

var text = "Blah Blah Blah SPECIAL TEXT: PART OF IT\n blah bah SPECIAL TEXT: STILL PART OF IT\n blah blah blah blah"

var arr = text.split("\n");

var newArray = arr.map(function(item){
  if(item.indexOf("SPECIAL TEXT") !== -1){
    return item.substring(0, item.indexOf("SPECIAL TEXT"));
   } else {
     return item
     }
});

text = newArray.join("\n");

alert(text);
nikhil
  • 107
  • 8
0
    var text = 'Blah Blah Blah SPECIAL TEXT: PART OF IT blah bah SPECIAL TEXT: STILL PART OF IT blah blah blah blah';  //save the text in a variable 

    var arr = text.split(" ")  /// make array with the words from the text

    arr = arr.filter(e => e=='Blah' || e=='blah') // filter the words

    arr = arr.join(' ')  // convert the array back to a string

Output: 'Blah Blah Blah blah blah blah blah blah'
Razvan Alex
  • 1,706
  • 2
  • 18
  • 21
  • Great, but it looses the line breaks –  Sep 18 '16 at 14:09
  • Hmm, I can implement and method that once the element is not equal with the string to break the line, reduce should be a better method in that case – Razvan Alex Sep 18 '16 at 14:11