How do I trim ugly lines correctly?

  • 0
    Parse (or scab ??) the page from which I want to pull the list of music albums. The problem is that album titles are listed as completely ugly strings. Something like this:
    'The Marshall Mathers LP 2'
    'Bangerz \ t \ t \ t \ t \ t \ t \ t \ n \ t \ t \ t \ t \ tDeluxe Version \ t \ t \ t \ t \ t (!) \ N \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t '│
    'Purpose \ t \ t \ t \ t \ tDeluxe \ t \ t \ t \ t \ t (!) \ N \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t'
    Okay, I thought. And he began to shred the lines.
    title = title.slice (0, title.indexOf ('\ t'))
    Basically, I only need the album titles, and it's deluxe or not - I don't care. Therefore, I shredded to the first encountered tabulation character. As a result, I got this:
    The Marshall Mathers LP
    Bangerz
    Purpose

    That is, the ugly lines were cut off as needed. But for lines without tabs, the last letter disappears. Why this happens, I do not understand. How can I achieve the desired result?

    P.S. made a bit of a mistake with the examples given. Corrected!
    JavaScript Nora Peck, Aug 30, 2019

  • 4 Answers
  • 0
    str.replace (/ \ t. * / s, '')
    Anonymous

  • 0
    Try this:

    let [title] = title.match (/ ^ [^ \ t | ^ \ n] + /);
    Nicholas Schmitt

  • 0
    [title] = title.split ('\ t');
    Anonymous

  • 0
    This is how String.slice () works, with considering that indexOf () returns -1 if the substring is not found.
    Colin Butler

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!