Global, Case-Insensitive replace()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #1

    Global, Case-Insensitive replace()

    [CODE=javascript]function repAll(t) {
    var finds = new Array(......); //array of to-be-replaced strings
    var repls = new Array(......); //array of replace-with strings
    t = t.replace(finds[i], repls[i], 'gi');
    }[/CODE]

    Working fine with FF, but not with IE, Opera and Safari...

    How can I solve this?
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Missed one line in the previous post.

    [CODE=javascript]function repAll(t) {
    var finds = new Array(......); //array of to-be-replaced strings
    var repls = new Array(......); //array of corresponding replace-with strings
    for (var i=0; i<finds.length ; i++)
    t = t.replace(finds[i], repls[i], 'gi');
    }[/CODE]

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      See this example.

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Originally posted by acoder
        I saw that already, but the difference is, they are using string itself, while I'm using reference. How could I add /gi to a referance?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Use the RegExp object, e.g. see this link.

          Comment

          Working...