2

According to regular-expressions.info, I should be able to remove duplicate lines (as long as they are consecutive) with the following pattern: ^(.*)(\r?\n\1)+$

This is pretty straightforward within Notepad++, I just hit "Replace all" with \1 and all the duplicates are removed. However, I cannot get it to work with javascript when pulling text from a <textarea>.

var t = $("#input").val();
var re = /^(.*)(\r?\n\1)+$/g;
var s = t.replace(re, "$1");
console.log(s);

Why has adding the g flag not removed all of the duplicate lines?

1
  • You can try this regex:- ^(.*)(?:(?:\r?\n|\r)\1)+$ Commented Oct 1, 2013 at 15:01

2 Answers 2

2

Use the multiline modifier as well:

var re = /^(.*)(\r?\n\1)+$/gm;

It seems that your input contains several lines and you want to remove some lines in the whole text which are duplicates.

One condition in that regex is that the definition of a 'line' is actually a string in its own line (there's no other string occupying any part of that line).

As such, the multiline modifier (m) will make the ^ and $ match the beginning and end of each individual line as opposed to the whole text.

Sign up to request clarification or add additional context in comments.

2 Comments

It seems to work without the m flag actually (fiddle), so not sure this is the problem.
@sp00m Nope, it's not working for me, see: the first link. But when I add the multiline more, I get second link
0

The correct replace call is:

var s = t.replace(re, "$1");

The replacement must be a string (or a function, for more complex stuff)

1 Comment

Ah, that was a typo that it wasn't in quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.