String.replace () with RegEx replaces incorrect characters

I am trying to use regular expression (javascript) to traverse periods in a text. The text looks like this:

This is a text with ending period on wrong line
.
This is a another line

      

I am using this regex:

summary.replace(/[\n\r]\.[\s\n\r]/gm, '.\r')

      

so it looks like this:

This is a text with ending period on wrong line.
This is a another line

      

But instead it looks like this:

This is a text with ending period on wrong line
.his is a another line

      

Can't figure out what is wrong with my regex.

Anyone?

+3


source to share


2 answers


Use this regex

[\n\r]+\.(?=[\s\n\r]+)

      



replace it with .

0


source


I would suggest you output the output encoded as JSON with JSON.stringify to see if there are any whitespace characters (like newlines) that still exist. I would also use quantifiers for your character classes so that it can match more than one character at once



/[\n\r]?\.[\s\n\r]*/gm

      

0


source







All Articles