The Spanner logo
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    The Spanner logo

    The Spanner
    Web security blog

    Made by Gareth Heyes
    Follow me on Twitter: @garethheyes

    Javascript for hackers!

    Hackvertor logo
    Shazzer logo
    My Github account
    Recent posts
    Pure-CSS 3D world collision detection How to write a Hackvertor tagIntroducing Feedworm: A Privacy-First RSS Reader That Lives in DevToolsSpeedy RSVP extensionAutoVaderHackvertor history and tag finderShadow Repeater v1.2.3 releaseBurp Hackvertor v2.1.24 releaseHacking roomsXSSing TypeErrors in SafarivalueOf: Another way to get thisMaking the Unexploitable Exploitable with X-Mixed-Replace on FirefoxThe curious case of the evt parameterCSS-Only Tic Tac Toe ChallengeRewriting relative urls with the base tag in SafariBypassing DOMPurify with mXSSNew IE mutation vectorHow I smashed MentalJSMentalJS DOM bypassAnother XSS auditor bypassXSS Auditor bypassBypassing the IE XSS filterUnbreakable filterMentalJS bypassesmXSSJava SerializationBypassing the XSS filter using function reassignmentRPOSandboxed jQueryX-Domain scroll detection on IE using focusEpic fail IEnew operatorDecoding complex non-alphanumeric JavaScriptHacking FirefoxDOM ClobberingBypassing XSS AuditorThe evolution of codeNon-Alpha PHP in 6-7 charsetTweetable PHP-Non AlphaMentalJS for PHP

    String replace JavaScript bad design

    By Gareth Heyes (@hackvertor)

    Published 15 years 10 months ago • Last updated October 2, 2025 • ⏱️ 2 min read

    ← Back to articles

    After using JavaScript for a while one of the worst parts I found was the String.replace function. When I realized it's behaviour I thought to myself someone is going to use this wrong. The function itself is excellent, I use it all the time as you could probably tell with my code. It is far better than some other languages with the ability to use strings/regexes and provide a function callback for the replacement.

    But...it's default behaviour is designed badly. When using replace most developers assume it works in global mode and the characters you intend to replace will all be replaced. Consider the following pitfall:-

    alert(':::'.replace(':',''));//only one : is replaced!

    What do you expect? No colons? You are not alone. Unfortunately by default replace assumes you only want to replace one character. Don't ask me why.

    To counteract this Mozilla decided to add a third argument! This is even worse! It makes is even more confusing. Now we have a function that accepts three arguments, first a string/regex, second a string/function and third a flag for the string replacement. We then have a situation where the replacement is global on Firefox and not on every other browser and to top it off, if you use the third flag with a regexp then the regexp won't of course be global thus adding yet more confusion!

    alert(':::'.replace(':','','g'));//replaces all ":" on Firefox but not on other browsers alert(':::'.replace(/:/,'','g'));//replaces one ":" as the flag only works for strings.

    A huge mess I'm sure you'll agree, IMO the replace function should work globally by default for string arguments with the option to match only once if for some crazy reason it is needed.

    The correct way of doing a global replacement is to use regexes because strings don't even allow you to do a global replacement on all browsers!

    alert(':::'.replace(/:/g,''));//The correct way

    Finally here is a patch that you can use to prevent your developers making the same mistakes a certain social network made.

    String.prototype.replace = (function(r){ return function(find, replace, replaceOnce) { if(typeof find == 'string' && !replaceOnce) { find = r.apply(find, [/[\[\]^$*+.?(){}\\\-]/g,function(c) { return '\\'+c; }]); find = new RegExp(find, 'g'); } else if(typeof find == 'object' && !replaceOnce && !find.global) { find = new RegExp(find.source, 'g'); } return r.apply(this, [find,replace]); } })(String.prototype.replace); alert('aaaabbbbb'.replace(/a/,''))

    ← Back to articles