0

I have the following 2 types of lat/long strings

2154S15002E

215422N1500232E

I wish to split the string on the N or the S, the following works but it drops the N or S so I get an rp array of 2154 15002E, when i want 2154S 15002E

var rp = rPoint.split(/\s*(?:N|S)\s*/);

How do I split and keep the ā€˜S’? Or is there a better way than split?

Thank you

tj26
  • 197
  • 1
  • 10
  • 1
    The duplicate uses lookahead to split before the delimiter but it's trivial to use lookbehind to get the split to occur *after* the delimiter instead. – Wyck Mar 06 '23 at 21:05

2 Answers2

0

You could just match the pattern of digits and letter.

console.log('2154S15002E'.match(/\d+[SE]/g));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Used lookbehind assertions to get split after N or S

var rp = rPoint.split(/(?<=[NS])/);
tj26
  • 197
  • 1
  • 10