Hi all,
can anyone give an example of regex which should not accept duplicate numbers separated with comma.
it should accept numbers like 1,2 and it should not repeat duplicates like 1,2,3,1,2,3
and also the number should terminate with a number like 1,2,3 and it should not terminate with , like 1,2, please help me
I don't think that a single regex can do this. You need something like:-
Code:
<html>
<head>
</head>
<body>
<script type = "text/javascript">
var val = "1,2,3,1,2,3,1,5,3,8,";
val = val.replace(/\,$/,""); // strip final comma if any
sval = val.split(","); //split string into an array
for (var i = 0; i<sval.length; i++) {
for (var j = i+1; j<sval.length; j++) {
if (i!=j) {
if ((sval[i] == sval[j]) && sval[i] !="") {
alert ("The value " + sval[i] + " array index " + i + " is duplicated at array index " + j);
sval[j] = ""; // delete duplicates
}
}
}
}
// ---------------------------------------------------------
var sval2 = [];
var j=0;
for (var i = 0; i<sval.length; i++) {
if (sval[i] != "" ) { // remove blanks (were duplicates) from the array
sval2[j] = sval[i];
j++;
}
}
var final = sval2.join(",");
alert (final);
</script>
</body>
</html>
Quizmaster: The Spanish Steps and the Colliseum lie in which Italian city?
Contestant: Athens.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 09-24-2012 at 01:03 PM..
Reason: Revised