troubleshooting Question

javascript extension methods?

Avatar of JosephEricDavis
JosephEricDavis asked on
JavaScriptjQuery
5 Comments1 Solution671 ViewsLast Modified:
Hey all, I have sort of a two part question.  What I'm trying to do is replace all the instances of a given substring within another string with another given substring.  The current replace method only seems to replace the first instance of the substring, which is pretty lame if you ask me.  Anyway, so my first question is...

Is there a way in javascript to do extension methods?  I am planning on writing a replaceAll method, but instead of writing it with a signature like this...

replaceAll(string, finder, replacer)

I would like it to be more like an extension method in C#...

string.replaceAll(finder, replacer)

I can handle the code inside of it, I just need to know how to extend an object in javascript.  I found this article...

http://codebetter.com/blogs/jeremy.miller/archive/2008/09/08/quot-extension-method-quot-in-javascript.aspx

Which talks about extending an array object, but how would I go about extending a string?

-----------------------

My second question...
Is there already a way in place in javascript to do a replaceAll like I am trying to do?  It seems like a pretty standard thing to do.  If there is, I would want to use that as my solution.  However, I am still interested in receiving an answer to my first question as well.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros