Skip to content
Advertisement

Split Function in Angular for a certain combination

I have a string which is like this:

  'Hellornand World 15.6n3'.

or it can be

  'Hellornand World 15.6nNA'.

and I want result which should split it like this:

  'Hellornand World 15.6'
  '3'.

The code which I have written:

  var lines = string.split('n');

which is producing result like this:

 'Hellor'
 'and World 15.6'
 '3'.

What changes should I make in split() function to get the desired result?

Advertisement

Answer

You can replace rn with something else, then split by n and put back the replaced rn:

'Hellornand World 15.6n3'.replaceAll('rn', '&newline').split('n').map(item => item.replaceAll('&newline', '\r\n'))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement