[thelist] regex help

Tom Dell'Aringa pixelmech at yahoo.com
Thu Sep 4 21:45:38 CDT 2003


--- Jeff Howden <jeff at jeffhowden.com> wrote:

> what language?  you don't specify, but from the syntax and what i
> know about the languages you use, i'd guess javascript.  if so, my 
> examples which will be based on javascript will be useful.

There are other languages than JavaScript? ;) (yes, JS)

> so, you have a string "s1s27s3s41s5s16s33" and you need to find and
> remove "s3" without also removing "s33".  a simple replace won't 
> work reliably in this case.  a regex would work, provided it's 
> written so that it doesn't get too greedy.  i think both of these 
> solutions are the wrong tool for the job.

I found that out...

> those that suggested treating it as an s-delimited list are on to
> something.
> 
>   myStudents = 's1s27s3s41s5s16s33';
> 
>   myStudent = '3';
>   myStudents = myStudents.substring(1).split('s');
>                // now equals 1,27,3,41,5,16,33
>   myStudentPos = myStudents.indexOf(myStudent);
>   myStudents = myStudents.deleteAt(myStudentPos);
>   myStudents = 's' + myStudents.join('s');
> 
> here's those array methods i use above.
> 
> function array_index_of(val)
> {
>   for(var i = 0; i < this.length; i++)
>   {
>     if(this[i] == val)
>       return i;
>   }
>   return -1;
> }
> 
> function array_delete_at(index)
> {
>   if(index < this.length)
>   {
>     for(var i = index; i < this.length - 1; i++)
>       this[i] = this[i + 1];
>     this.length = this.length - 1;
>   }
>   return this;
> }
> 
> Array.prototype.indexOf = array_index_of;
> Array.prototype.deleteAt = array_delete_at;

Thanks jeff - I ended up using something fairly similar to this. I
have to revisit the page again (which is why I have not answered any
threads) and see how I did it (it's been hairy, I can't remember).
Luckily they have gotten saner with how the page should work and I
may not even have to do it anymore :) But hey, its a learning
experience...

Thanks everyone else for you comments as well.

Tom

=====
http://www.pixelmech.com/ :: Web Development Services
http://www.DMXzone.com/ :: Premium Content Author / JavaScript / Every Friday!
http://www.maccaws.com/ :: Group Leader
[Making A Commercial Case for Adopting Web Standards]

"That's not art, that's just annoying." -- Squidward


More information about the thelist mailing list