Exploring
Exploring

Reputation: 3429

how to remove unicode new line from javascript string

I was replacing new line with the following javascript code:

str.replace(/(\r\n|\n|\r)/gm," ")

However, came across some Unicode new line Unicode Character 'NEXT LINE (NEL)' (U+0085).

How to remove new lines from a string safely i.e. all these weird new lines will be removed as well?

Is it an established API for javascript?

Upvotes: 0

Views: 819

Answers (1)

cisco
cisco

Reputation: 888

Unicode in regular expressions with \u

You can reference unicode characters by prepending \u to the unicode sequence. So U+0085 -> \u0085

Example:

const str = 'Need space here ->\u0085<-';
str = str.replace(/\u0085/g, ' ');
console.log(str)
// Output: Need space here -> <-

Here a nice read by flavio if you want to further explore dealing with Unicode chars: https://flaviocopes.com/javascript-unicode/

Hope this helps

Upvotes: 1

Related Questions