Javascript: filter repeating character...

J

Joseph

I tried to find a script that would filter out repeating characters before
saving the string. But no luck so far.

For exemple, if a user writes 'haaaaaaaaaaaaaaa', i would like to get
"haaa". If he writes "!!!!!!!!!!!!!", i would like "!!!"...

Any suggestions?

Thanks

Tromal
www.tromal.net
 
L

Lasse Reichstein Nielsen

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("");
}
 
R

RobG

Lasse said:
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("");
}


Or you could try:

function triChar(s) {
var a = new Array();
for (var i=0; i<s.length; i++){
a.push(s.charAt(i));
if (s.charAt(i) == s.charAt(i+1)
&& s.charAt(i) == s.charAt(i+2)){
a.push(s.charAt(i+1),s.charAt(i+2));
i += 2;
while(s.charAt(i) == s.charAt(i+1)) i++;
}
}
return (a.join(''));
}

Have a good one... Zif.
 
F

Fred Oz

RobG wrote:
[...]
Or you could try:

How about:

function triChar(s) {
var a = s.split('');
for (var i=0; i<s.length; i++){
if (a == a[i+1] && a == a[i+2]){
i += 2;
while(a == a[i+1] && i<a.length) a.splice(i,1);
}
}
return (a.join(''));
}

You can also do a unique character filter using splice():

function uChar(s) {
var a = s.split('').sort();
for (var i=0; i<a.length; i++) {
while (a == a[i+1]) a.splice(i,1);
}
return a.join('');
}

Fred.
 
J

Joseph

Thanks to all for your code... its exactly what I was looking for!

This script will be executed just before submitting a form.

Thanks again!

Tromal
www.tromal.net
 
L

lawrence

RobG said:
function triChar(s) {
var a = new Array();
for (var i=0; i<s.length; i++){
a.push(s.charAt(i));
if (s.charAt(i) == s.charAt(i+1)
&& s.charAt(i) == s.charAt(i+2)){
a.push(s.charAt(i+1),s.charAt(i+2));
i += 2;
while(s.charAt(i) == s.charAt(i+1)) i++;
}
}
return (a.join(''));
}



I know I'm being stupid, but what does the first a.push(s.charAt(i));
do? The script would work fine without that line, wouldn't it?
 
R

RobG

lawrence said:
I know I'm being stupid, but what does the first a.push(s.charAt(i));
do? The script would work fine without that line, wouldn't it?

No. It pushes the current character s into a. Otherwise, the only
time anything will get pushed from s is when the if is true. So you
would end up with a containing only the three (or more) character
strings from s truncated to three characters.

Cheers, Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top