Author Topic: Replace Duplicate Unknown From a String [JS]  (Read 2346 times)

0 Members and 1 Guest are viewing this topic.

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
    • Facebook
Replace Duplicate Unknown From a String [JS]
« on: December 28, 2007 »
Hrrms. I was wondering whether there is away to remove an unknown duplicate text from a string in JavaScript?
I had a look on the net (google), but they are all replacing using arrays.
Now, my situation:
With my gadget, I am using JavaScript (ajax) to pull information from our server. This information is updated as each new song plays (the information is the title and artist).
Now. I have no idea what will be pulled from the ajax Get, so I can't compare them. Also, I am not putting the pulled information into an array. The string would look like this:
KIK FM, DARWIN NT [song name] - [song artist] [song name] - [song artist]
This is because the file I am pulling from is a html file with unique tags within the HEAD (the tags allow our SMS system to pull the songs information), as well as the information being displayed within the BODY (for the website).
I have been able to remove all the html tags so they don't appear in the string. But, I don't like the way the string is comprised of both the HEAD and BODY information! I tried the match[body] function (can't remember it) but it returned NULL.

Is there anyway to remove the second song name and artist - without the use of arrays and a For Loop?

Thanks in advance! I've almost completed v1.41 of the gadget (I know, 1.41 already!!! But yeah. Each change is a change in version) lol
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4380
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Replace Duplicate Unknown From a String [JS]
« Reply #1 on: December 28, 2007 »
Could be done with regular expression IMHO. Have a look at the following link for example
or try a search on google for javascript and regular expressions :

http://www.javascriptkit.com/javatutors/redev2.shtml

http://www.regular-expressions.info/javascriptexample.html

[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
    • Facebook
Re: Replace Duplicate Unknown From a String [JS]
« Reply #2 on: December 28, 2007 »
--what's IMHO? lol... is that like FYI (For Your Information), or is it related to JavaScript? I'm guessing the latter.

Thanks for the links! Great read lol. I've always wanted to know why I had to put:
Code: [Select]
replace(/<\/?[^>]+(>|$)/g, "");
to remove the html tags haha

Had a look, and I couldn't find anything (I may have missed something) which could help. The examples were 'matching' specific, user pre coded characters. As I mentioned, the string obtained will always be different (as in it is unpredictable) if the song has changed, therefore I cannot pre code to remove the second entry (with reading those pages... I think...)
Although, the second link shows an example of comparison...
Q: does the string have:
Code: [Select]
\bt[a-z]+\b
A: press the button...

My aim with this questions is to see if I can remove the repeated song name and artist within the string. I already know that the string will have duplicate entries, I just want to be able to remove the second occurrence, without actually knowing what the characters of the string are comprised of... if that helps?
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Replace Duplicate Unknown From a String [JS]
« Reply #3 on: December 28, 2007 »
IMHO = In My Humble Opinion.
Can you post an exact sample string for us to look at?
It should be possible to extract the song name, and then find and replace any later song names with blanks using replace.

Jim
Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
    • Facebook
Re: Replace Duplicate Unknown From a String [JS]
« Reply #4 on: December 28, 2007 »
lol, ok nice :P

Here is the JavaScript AJAX function to get the information from the html file:
Code: [Select]
function ajaxGet()
{
if (status == true) {
if (req == null)
req = new XMLHttpRequest();

if (req) {
req.onreadystatechange = infoReceived;
req.open("GET", trackURL, true);
req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
req.send(null);
}
var ajaxGain = setTimeout("ajaxGain()", updateInterval);
}
}

function ajaxGain()
{
req = null;
clearTimeout(ajaxGain);
ajaxGet();
}

function infoReceived()
{
if (req.readyState == 4) {
if (req.status == 200) {
// var output = req.responseText.match(/<body>(.*?)<\/body>/g);
var output = req.responseText;
output = output.replace(/<\/?[^>]+(>|$)/g, "");
if (song != output) {
song = output;
pos = 0;
clearTimeout(timer);
startScroll();
}
} else {
song = "Track Server Error: " + req.status + ", Response: " + req.statusText;
}
}
}

Here is the html file code (located on our server):
Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>KIK FM, Darwin NT    </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Refresh" content="15">

<comment><!--SMSTKSTART-->{PL1_SONG?} - {PL1_ARTIST?}<!--SMSTKEND--></comment>
</head>

<body>
<div id="information" align="center">
<tt><b>     {PL1_SONG?} - {PL1_ARTIST?}</b></tt>
</div>
</body>
</html>

<!-- NOTE: the use of {PL1_SONG?} - {PL1_ARTIST?} is necessary for us to change their values to the songs name and artist upon a new event (song changes) -->

The ajax replaces all html tags with "", therefore leaving:
Code: [Select]
KIK FM, Darwin NT    {PL1_SONG?} - {PL1_ARTIST?}     {PL1_SONG?} - {PL1_ARTIST?}
Remembering that the {} will not be present in the html file, as they will be overridden by the song name and artist.

This means that the pulled string will end up looking like (my favourite song as an example :)):
Code: [Select]
KIK FM, Darwin NT    Light the Skies (Retrobyte Electrobounce Mix) - Cerf, Mitiska, & Jaren     Light the Skies (Retrobyte Electrobounce Mix) - Cerf, Mitiska, & Jaren

EDIT
OMG!!!! The forum removed the duplicate entry... hmmm... not sure why that happened, I put them in the [cod3]... hmmmm... anyone know how this board runs the posting? lol!
It should have:
Code: [Select]
Light the Skies (Retrobyte Electrobounce Mix) - Cerf, Mitiska, & Jaren
After the first one too...

/EDIT

As we all know, JavaScript and HTML will not show the multiple 'spaces'

But JavaScript (I have found) pauses between the large spaces... a nice effect (hopefully no full of memory leaks, slowdowns and bad bugs are caused by this) for the gadget, as it shows the KIK FM bit, scrolls, pauses at the first character of the song name, scrolls through the first entry (song name, song artist) and pauses again haha.

Hope thats enough information for you guys to use. :)
Thanks for your help, as always :P
« Last Edit: December 28, 2007 by Clanky »
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..