jquery find / replace arrays?

Posted on 2011-10-09
Last Modified: 2012-05-12
I'm trying to learning about pattern matching with jQuery and I had a question...

In php, I can do something like this:

$find = array("-", "_", "+");
$replace = array("X", "Y", "Z");

$string = "The alphabet ends in -, _, +";
$newstring = str_replace($find, $replace, $string); 

Open in new window

Which will yield: The alphabet ends in X, Y, Z

Is there a way to accomplish this with jquery?  Where I can define an array of values to search for and replace them with the values in another array?  I get that I can do a replace(/pattern/, "Something"), but was hoping for a way to duplicate this php code in jquery?
Question by:n00b0101
    LVL 40

    Assisted Solution

    LVL 17

    Accepted Solution

    Look at below code.
    Test Page :

    The plus sign is used in regular expressions to indicate 1 or more characters in a row. So you should use escape

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
        var text = "The alphabet ends in -, _, +";
        var array = {"-":"X", "_":"Y", "\\+":"Z"};
        for (var val in array)
          text = text.replace(new RegExp(val, "gi"), array[val]);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now