-2

I'm trying to parse an email body and get the next 3 lines after the word "Location" in javascript.

Here is my text:

Blah blah other email content

Location:
1600 Pennsylvania Avenue
Washington, DC 20006
USA

So the first thing I know to do is use indexOf to parse for "Location" but how do I get the three lines after that? Do I have to do a "split" of all lines in the text area and then use "\n" to somehow go to the next few lines? Do I have to use RegEx?

  var a = textarea;
  var b = a.split("\n");
  var c = b.indexOf("Location");

2 Answers2

1

This should do:

var text = document.getElementById("input").value;

var stripped = text.replace(/[\s\S]+Location:[\r\n]+/gi, '');
var addressLines = stripped.trim().split(/[\r\n]+/);

console.log(addressLines);
<textarea id="input">
Blah blah other email content

Location:
1600 Pennsylvania Avenue
Washington, DC 20006
USA
</textarea>
  1. Remove everything before and including Location:
  2. Split the remaining text on newlines.
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • what if text exist after third line `country name : USA` ? `Location: 1600 Pennsylvania Avenue Washington, DC 20006 USA and this extra line more extra line` – Niklesh Raut Sep 01 '17 at 07:23
  • The OP gave no indication that's a possibility, but any lines after USA will be array entries in the result. Which may be a good thing if there's a address-related line after `USA`. – Cerbrus Sep 01 '17 at 07:25
  • yes you are right, I also though same , don't know who don't like my answer – Niklesh Raut Sep 01 '17 at 07:26
0

If you split the text by \n and/or \r using a regex as the answer by @Cerbrus does (as lines), you can loop line by line using an index (i) to access the text in the current line.

Once you have verified that the line contains Location (using line.indexOf('Location') !== -1), you can then get the following three lines using lines.slice(i + 1, i + 4).

function getLocation() {
    var textarea = document.getElementById('text');
    var lines = textarea.value.split(/[\r\n]+/);
    for(var i = 0; i < lines.length; i ++) {
        var line = lines[i];
        if(line.indexOf('Location') !== -1) {
            var nextThreeLines = lines.slice(i + 1, i + 4);
            console.log(nextThreeLines);
        }
    }
}
#text {
    height: 150px;
    width: 250px;
}
<textarea id="text">
Blah blah other email content

Location:
1600 Pennsylvania Avenue
Washington, DC 20006
USA
</textarea>
<br>
<button onclick="getLocation()">Get Location</button>
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
  • Can you provide any feedback? – Moishe Lipsker Sep 01 '17 at 07:22
  • Please do not re-post your answers to get around downvotes. Such actions are frowned upon, and may even result in a answer ban if you do it regularly. – Cerbrus Sep 01 '17 at 07:22
  • Feedback: `for` loop that iterates over _all_ lines, regardless of if a result has been found already. Splitting on a `"\n"` literal instead of `/[\r\n]/` may be a compatibility concern. – Cerbrus Sep 01 '17 at 07:27
  • @Cerbrus Thank you for the warning and the feedback. Could you share a use case that would break the above example? – Moishe Lipsker Sep 01 '17 at 07:32
  • @Cerbrus I've updated the answer to also take `/r` into account. Could you share any other parts of the answer that you think I can improve? – Moishe Lipsker Sep 01 '17 at 07:51
  • You're aware that some systems use `\r\n`? Your code wouldn't work for those. Try this: `"foo\r\nbar".split(/\r|\n/)`. – Cerbrus Sep 01 '17 at 07:55
  • @Cerbrus I've updated the answer once again. Thank you for helping me improve the answer. – Moishe Lipsker Sep 01 '17 at 08:07
  • thanks @Cerbus your code worked for me! the only thing i had to do was put a join() in the nextThreeLines array because I wanted to return a string instead of array! All set! – user1974586 Feb 06 '18 at 05:46