Reputation: 15372
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?
Upvotes: 2
Views: 1844
Reputation: 71538
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.
Upvotes: 2
Reputation: 324630
The correct replace call is:
var s = t.replace(re, "$1");
The replacement must be a string (or a function, for more complex stuff)
Upvotes: 0