473,903 Members | 3,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript: filter repeating character...

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 'haaaaaaaaaaaaa aa', i would like to get
"haaa". If he writes "!!!!!!!!!!!!!" , i would like "!!!"...

Any suggestions?

Thanks

Tromal
www.tromal.net

Jul 23 '05 #1
6 3264
"Joseph" <mi**@moimiom.o rg> writes:
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 'haaaaaaaaaaaaa aa', 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(str ing) {
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("ba hhhhhhlamb!!!!! !!1223334444555 55")
---
yields
---
"bahhhlamb!!!12 2333444555"
---
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 manyToThreeSimp le(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[i] = c;
}
}
return accumulator.joi n("");
}
---

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
Lasse Reichstein Nielsen wrote:
If it is for internet use, you will have to traverse the string yourself:
---
function manyToThreeSimp le(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[i] = c;
}
}
return accumulator.joi n("");
}


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.
Jul 23 '05 #3
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[i] == a[i+1] && a[i] == a[i+2]){
i += 2;
while(a[i] == 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('').sor t();
for (var i=0; i<a.length; i++) {
while (a[i] == a[i+1]) a.splice(i,1);
}
return a.join('');
}

Fred.
Jul 23 '05 #4
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

"Joseph" <mi**@moimiom.o rg> wrote in message
news:Fw******** *********@weber .videotron.net. ..
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 'haaaaaaaaaaaaa aa', i would like to get
"haaa". If he writes "!!!!!!!!!!!!!" , i would like "!!!"...

Any suggestions?

Thanks

Tromal
www.tromal.net

Jul 23 '05 #5
RobG <rg***@iinet.ne t.auau> wrote in message news:<OU******* **********@news .optus.net.au>. ..
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?
Jul 23 '05 #6
lawrence wrote:
RobG <rg***@iinet.ne t.auau> wrote in message news:<OU******* **********@news .optus.net.au>. ..
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?


No. It pushes the current character s[i] 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.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
5778
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
2
1243
by: Aaron | last post by:
how would i use document.write to write this input box? i can do it without quotes but i need them for my purpose <input type="textbox" name="input" value="something"> i did this but it doesn't have the double quotes document.write( '<input type="textbox" name="input" ' + 'value="' + entereddata+ '">'); 2.
6
6077
by: Chris | last post by:
Can anyone point me to some code that will display messages, in a seperate window, from javascipt. ie effectively a trace window? TIA Chris
1
4344
by: Julia | last post by:
Hello there. I have a question somewhat related to this topic, and I don't know where else to go. I hope somebody can help. I've created a database in access, that I'd like to share with less than 10 users. I'd like each user to see only some of the records in the database. So for example, if I have eight users in eight countries, I want each of them to only see information for their country. I was told that the easiest way to do...
4
3525
by: John Boy | last post by:
Hi, Can anyone help. This is really doing my nut in. 3 years ASP exp. and now doing .DOT which is a step in the wrong direction. Basically I am left with the code of a guy who has left. When I click a button on a pop-up window the javascript for that button click does a 'button.form.submit'. On the Server side there is a Button click event for this button, but for some reason it no longer fires. It worked fine before and everything...
104
17056
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
11
2801
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or < or several others, it converts them to html, such as &amp; or &lt; which can sometimes cause my scripts not to work. How can I prevent ASP.NET from doing this? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
3
2712
by: Tom | last post by:
Basically I have a page that I load with 10 input fields. If users have JS enabled I want to hide 5 of these fields so as to reduce clutter. If the user needs these extra fields an "Add" button can be used to display the hidden fields one by one. Once the fields a filled in the user submits them for validation and if there are any error They can "Go back" to make some changes. The whole process works great in FF & Opera but IE lets me...
1
1950
by: sheldonlg | last post by:
I have inherited code with a TDC control. In this file, there are two javascripts of interest. One of these is a function, filter(), which is inside <script language=javascript></script>. The other is code inside <script language=javascript FOR=inquiry event=ondatasetcomplete()></script> where inquiry is the TDC.
0
9999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9847
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10500
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9684
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7206
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.