1

I had a text field and we can copy and paste url on it. Sometimes user are pasting the url twice like

"http://www.google.com/test/index.htmlhttp://www.google.com/test/index.html". 

How can i find out that url is duplicated.

I tried using below code

var url="http://www.google.com/test/index.htmlhttp://www.google.com/test/index.html";

var exp =/https?:\/\//gi;

alert(url.match(exp).length);

If length is greater than 1 then it is duplicate. Trying for the any best option to find duplicate.

BetaRide
  • 16,207
  • 29
  • 99
  • 177
CNKR
  • 568
  • 5
  • 19

4 Answers4

0

I suggest using a better regex. You do not need to check for each possible problem separately, if the user input does not match the regex then it is not valid. Try the following:

^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$
dchar
  • 1,665
  • 2
  • 19
  • 28
0

If client always paste url with http: prefix simply count the ':' occurrence in String. If its more than one you will know that it has wrong format.

Some helper of counting chars in Java: How do I count the number of occurrences of a char in a String?

Community
  • 1
  • 1
graczun
  • 572
  • 1
  • 5
  • 16
  • Some times url may be like www.google.com with out protocol(like http/https:) then i need to add the protocol. – CNKR Sep 02 '15 at 07:35
  • so client can paste something like www.google.comwww.google.com and AFAIK it could be valid URL (strange, but compatible with URL semantics). You can always count 'www.' and if it has more than 1 occurence throw exceptions. But it wouldn't work in all scenarios ( for example www.comwww.com). In this case I would not consider my answer as valid one. – graczun Sep 02 '15 at 07:44
  • @user3498863: Do not add details in the comments for an answer, you should update the question. In case you are *sure* the pasted string is URL and you need to use [something like this](http://jsfiddle.net/t6zL18qL/1/). Please let me know if it works for you, I will post then. – Wiktor Stribiżew Sep 02 '15 at 07:58
0

If you're just trying to detect someone double pasting, you can use string.length and string.substring to detect the duplicate, eg:

if (url.length % 2 === 0 && url.substring(0, url.length/2) === url.substring(url.length/2, url.length/2)) {
    alert('Dupe');
}

That said, if you're trying to detect duplicates that also match a pattern, I guess you can use pattern capture groups and backreferences, like:

/^(https?:\/\/.*)\1$/
Jon Marnock
  • 3,155
  • 1
  • 20
  • 15
  • The problem will arise if the URL is tripled, or quadrupled. Also, OP says the URL can start with no protocol at all. – Wiktor Stribiżew Sep 02 '15 at 07:59
  • Yep, OP didn't mention triples etc, just occasional doubling. If it needs to detect arbitrary repeats, regex is ~definitely~ not the right way to go. Re being able to deal with no protocol at all, that's fine - the part inside the brackets was just meant to be a placeholder which OP can replace with whatever regex they prefer to match a valid URL. (I specifically copied the regex OP provided in their question) – Jon Marnock Sep 02 '15 at 08:01
0

I sugges using /^(\S+)\1+$/ to match any characters from the beginning of the string with (\S+), and then use \1+ backreference to see if the sequence is duplicated, or tripled, or quadrupled (+ makes this repetition possible).

Thus, you can use

function sanitizeUrl(urls) {
    var uniq = urls.match(/^(\S+)\1+$/);
    if (uniq !== null) {
       return uniq[1].substring(0, 4) !== "http" ? "http://" + uniq[1] : uniq[1];
    }
}

var urls = "google.com/test/index.htmlgoogle.com/test/index.html"; // No HTTP
document.getElementById("r").innerHTML = sanitizeUrl(urls) + "<br/>";
urls = "http://www.google.com/test/index.htmlhttp://www.google.com/test/index.htmlhttp://www.google.com/test/index.html";
document.getElementById("r").innerHTML += sanitizeUrl(urls);
<div id="r"/>

The uniq[1].substring(0, 4) !== "http" ? "http://" + uniq[1] : uniq[1] logic either adds http:// or not depending on the result obtained.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you. [See How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow) – Wiktor Stribiżew Sep 02 '15 at 10:21