Joseph said:
I tried to find a script that would filter out repeating characters before
saving the string. But no luck so far.
What do you mean by "saving"?
For exemple, if a user writes 'haaaaaaaaaaaaaaa', i would like to get
"haaa". If he writes "!!!!!!!!!!!!!", i would like "!!!"...
This only works in Javascript versions that are ECMA 262 v3 compatible,
since it uses the enhanced regular expressions:
---
function manyToThree(string) {
return string.replace(/(\S)(\1{2,})/g,"$1$1$1");
}
---
That function should replace repetitions of three or more
non-whitespaces by only three. E.g.
---
manyToThree("bahhhhhhlamb!!!!!!!122333444455555")
---
yields
---
"bahhhlamb!!!122333444555"
---
Enhanced regular expressions are still not working in all browsers in
current use, so it's probably too soon to use them for the internet.
If you use Javascript on the server, or in a controlled environment,
then it should work.
If it is for internet use, you will have to traverse the string yourself:
---
function manyToThreeSimple(string) {
var accumulator = new Array();
var current = "";
var count = 0;
for (i = 0;i < string.length; i++) {
var c = string.charAt(i);
if (c == current) {
count++;
} else {
count = 1;
current = c;
}
if (count <= 3) {
accumulator
= c;
}
}
return accumulator.join("");
}