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
    Introducing 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 PHPOpera x domain with video tutorialSandboxing and parsing jQuery in 100ms

    Javascript unique strings with RegExps

    By Gareth Heyes (@hackvertor)

    Published 17 years 4 months ago • Last updated March 22, 2025 • ⏱️ 2 min read

    ← Back to articles

    I wrote a cool new feature for Hackvertor which found unique strings in some text, basically the function took a argument of a regular expression to split the text into parts and then scan a dynamic reg exp to check if the strings were unique are not.

    I thought I'd run through the code as a change from my usual posts which are usually about hacking javascript.

    <pre lang="javascript"> function unique(regexp, code) { code = code.split(regexp); var result = []; var found = ""; for(var i=0;i<code.length;i++) { if(!new RegExp(found).test(code[i]) || found == '') { var escaped = code[i].replace(/([\\.^$*+?{}\[\]\|\(\)!])/g,"\\$1"); if(/^[\d\w]/.test(escaped)) { escaped = '\\b' + escaped; } if(/[\d\w]$/.test(escaped)) { escaped = escaped + '\\b'; } result.push(code[i]); if(found != '') { found += '|'; } found += escaped; } } return result.join(","); } alert(unique(/\s+/, 'test test test test1 test1 test2 test2 test2 test3 test2 test3')); </pre>

    I used RegExp.test because it returns true or false and is much faster than match. The found string builds a dynamic RegExp of all the strings matched and checks their boundaries if they end or start with numbers or text. This is to stop parts of strings being matched and allowing strings within quotes etc. I also escape any RegExp characters matched in the text and return a string of the matches separated by commas.

    Here is the Hackvertor tag in action:- Hackvertor unique tag

    ← Back to articles