Not logged in. · Lost password · Register
Forum: Skins RSS
Remove Artist Name from Song Title
Avatar
Azimuth #1
Member since Jan 2003 · 427 posts · Location: Chicago, Illinois - USA
Group memberships: Members
Show profile · Link to this post
Finally got around to doing this...
    <script language="JavaScript" type="text/JavaScript">
  myString = new String("<#songtitle>")
  results = myString.replace("<#artist> - ", "")
  document.write(results)
    </script>
Just put this in place of <#songtitle>.
Avatar
Tom W.M. #2
Member since Feb 2003 · 40 posts · Location: La Crosse, WI, USA
Group memberships: Members
Show profile · Link to this post
Here's a more compact version:
<script type="text/javascript">document.write(("<#songtitle>").replace("<#artist> - ", ""));</script>
Of course, if you're doing this with proper DOM methods, it should be,
var titletext = document.createTextNode(("<#songtitle>").replace("<#artist> - ", ""));
the node can then be appended to an element, etc.  Of course this block must be contained in /*<![CDATA[*/ JS code /*]]>*/ in any X(HT)ML document--otherwise stray ampersands can cause parsing errors. (I find that, in XML, it is best to put anything that BrowseAmp writes in a CDATA section.  The problem with this technique is that it doesn't work for attribute nodes--there I must set urlencode=true, and dechipher the result with JavaScript.)

It's funny--I didn't realize that replace() could be used with anything but a regular expression, before.  So I checked the reference, and found out that functions can be used as the second argument.  Neat, huh?
Avatar
Azimuth #3
Member since Jan 2003 · 427 posts · Location: Chicago, Illinois - USA
Group memberships: Members
Show profile · Link to this post
Nice one Tom!
Avatar
Azimuth #4
Member since Jan 2003 · 427 posts · Location: Chicago, Illinois - USA
Group memberships: Members
Show profile · Link to this post
Taking that into consideration, and combining that with some bad tags in my library, I've started to use this...

        results = new String("<#songtitle>").replace(/<#artist>/g, "").replace(/-/g, "")
        document.write(results)

Switching from quotes to forward slashes allows the g modifier, which replaces all occurrences of the string to be replaced.
Avatar
Tom W.M. #5
Member since Feb 2003 · 40 posts · Location: La Crosse, WI, USA
Group memberships: Members
Show profile · Link to this post
It should be noted, that that the above code creates the variable results as a string object, not a regular string primitive.  Trying to use a string object as a string primitive may cause someone to encounter strange issues, as they behave differently; there's no real reason to use a string object in this case.

I submit following code, which should strip any occurences of the <#artist> string, along with any leading hypens and whitespace.  In addition, whitespace is normalized to a single space, and leading and following whitespace is stripped. For the replacement of the artist, the literal RegExp notation has been dropped, in favor of object notation.  This is because (in my experience), forward slashes are fairly common in metadata (especially for classical music), whereas double quotation marks are quite uncommon--this means fewer errors caused by illegaly terminated literal regular expressions.

var results = ("<#songtitle>").replace(new RegExp("^([ \t\v\n\r\f]{0,}<#artist>[ \t\v\n\r\f]{0,}-[ \t\v\n\r\f]{0,})+","gi"),"").replace(/\s+/g," ").replace(/^ | $/g,"");

There is one limitation: the artist name must be followed by a hyphen (this could easily be any character or set of characters), or it will not be replaced.  This is to avoid replacing the entire song name, if the song has the same name as the artist.

I have added the i modifier to the regular expression for replacing the artist--case insensitivity seems like a good idea to me.
This post was edited on 2005-07-27, 11:42 by Unknown user.
Avatar
Azimuth #6
Member since Jan 2003 · 427 posts · Location: Chicago, Illinois - USA
Group memberships: Members
Show profile · Link to this post
Interesting. When I paste that in place of my line, it displays nothing.  :o

Do you have a link for a page that references all this? I have not seen anything like you are suggesting yet. I just mess with it 'til it works!  ;)
Avatar
Tom W.M. #7
Member since Feb 2003 · 40 posts · Location: La Crosse, WI, USA
Group memberships: Members
Show profile · Link to this post
That's probably because of a typo on my part: you wrote results, but I wrote var result!  Change result to results, and you should be fine. Or, just use the following line, which does away with the variable altogether:
<script type="text/javascript">document.write(("<#songtitle>").replace(new RegExp("^([ \t\v\n\r\f]{0,}<#artist>[ \t\v\n\r\f]{0,}-[ \t\v\n\r\f]{0,})+","gi"),"").replace(/\s+/g," ").replace(/^ | $/g,""));</script>
I have fixed the code in my original post.
This post was edited on 2005-07-27, 11:41 by Unknown user.
Avatar
Tom W.M. #8
Member since Feb 2003 · 40 posts · Location: La Crosse, WI, USA
Group memberships: Members
Show profile · Link to this post
Oh, and there's a missing parenthesis after document.write in the code example above.  Could you fix that too?  I really wish that there was some sort of edit functionality...
Avatar
Azimuth #9
Member since Jan 2003 · 427 posts · Location: Chicago, Illinois - USA
Group memberships: Members
Show profile · Link to this post
Edits made. I thought a person could edit their own posts.
Avatar
Tom W.M. #10
Member since Feb 2003 · 40 posts · Location: La Crosse, WI, USA
Group memberships: Members
Show profile · Link to this post
Thanks, Azimuth.  It is strange that I couldn't edit them--I know that Invision Power Board can do editing.  I guess that it must be disabled for some reason.

I've added a page to my website with a live demo of replacements.
Avatar
Henry (Administrator) #11
Member since Jan 2003 · 865 posts · Location: Munich Germany
Group memberships: Administrators, Members
Show profile · Link to this post
There was an edit time restriction of 10 minutes. I have just removed that restriction for registered members.
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Page created in 237.3 ms (134.7 ms) · 89 database queries in 112.8 ms
Current time: 2018-02-25, 05:16:57 (UTC +00:00)