Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

string.replace() with RegEx replaces wrong characters

string.replace() with RegEx replaces wrong characters

Problem

I am trying to use a Regex (javascript) to move 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 use this regex:

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

to make it look 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

Cant figure out what is wrong in my regex.

Anyone?

Problem courtesy of: jonasonline

Solution

Use this regex

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

replace it with .

Solution courtesy of: Anirudha

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

string.replace() with RegEx replaces wrong characters

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×