{"id":609,"date":"2018-03-11T20:39:34","date_gmt":"2018-03-11T18:39:34","guid":{"rendered":"https:\/\/html-online.com\/articles\/?p=609"},"modified":"2023-12-20T13:57:02","modified_gmt":"2023-12-20T11:57:02","slug":"random-string-pattern-generator-javascript","status":"publish","type":"post","link":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/","title":{"rendered":"Random string pattern generator in JavaScript"},"content":{"rendered":"<p><strong>In this article I&#8217;m going to present a JavaScript code that generates a random string in a certain pattern. The <a href=\"https:\/\/html-css-js.com\/js\/editor\/\">JS function<\/a> takes a string that matches the given pattern and returns a random string that contains the same type of characters in the same order as the sample.<\/strong><br \/>\n<img decoding=\"async\" class=\"aligncenter\" src=\"\/articles\/wp-content\/uploads\/2018\/03\/random-string-pattern-generator-javascript.jpg\" alt=\"random string generator js function\"><br \/>\n<!--more--><\/p>\n<h2>Example<\/h2>\n<p><strong>Input<\/strong>:&nbsp;<em>AA aaa1111 ### Aaaaaa<br \/>\n<\/em><strong>Interpreted as<\/strong>: Two capital letters, one space, three lowercase letters,&nbsp;four numbers, three special characters and a capitalized six-letter word.<br \/>\n<strong>Example result that matches the pattern:&nbsp;<\/strong><em>XC tmr4990 %&amp;@ Yvhwfd<\/em><\/p>\n<p>I could have specified&nbsp;<em>AB abc1234 !@# Abcdef<\/em> for the input or any other string that matches the desired result.<\/p>\n<h2>The JavaScript Code<\/h2>\n<p>The function below distinguishes lower and capital letters, numbers and handles everything else as special characters but you can tweak the code to match your needs.<\/p>\n<pre style=\"background:#fff;color:#000\">function <span style=\"color:#0000a2;font-weight:700\">randomStringPattern<\/span>(input) {\n  var text <span style=\"color:#687687\">=<\/span> <span style=\"color:#d80800\">\"\"<\/span>;\n  var possible;\n  <span style=\"color:#0100b6;font-weight:700\">for<\/span> (var j <span style=\"color:#687687\">=<\/span> <span style=\"color:#cd0000;font-style:italic\">0<\/span>; j <span style=\"color:#687687\">&lt;<\/span> input.<span style=\"color:#06960e;font-weight:700\">length<\/span>; j<span style=\"color:#687687\">++<\/span>) {\n    <span style=\"color:#0100b6;font-weight:700\">if<\/span> (input[j] <span style=\"color:#687687\">==<\/span> <span style=\"color:#d80800\">\" \"<\/span>) {\n        possible <span style=\"color:#687687\">=<\/span> <span style=\"color:#d80800\">' '<\/span>;\n    } <span style=\"color:#0100b6;font-weight:700\">else<\/span> <span style=\"color:#0100b6;font-weight:700\">if<\/span> ((input[j] <span style=\"color:#687687\">==<\/span> input[j].<span style=\"color:#3c4c72;font-weight:700\">toUpperCase<\/span>()) <span style=\"color:#687687\">&amp;<\/span><span style=\"color:#687687\">&amp;<\/span> (input[j] <span style=\"color:#687687\">!<\/span><span style=\"color:#687687\">=<\/span> input[j].<span style=\"color:#3c4c72;font-weight:700\">toLowerCase<\/span>())) {\n        possible <span style=\"color:#687687\">=<\/span> <span style=\"color:#d80800\">\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"<\/span>;\n    } <span style=\"color:#0100b6;font-weight:700\">else<\/span> <span style=\"color:#0100b6;font-weight:700\">if<\/span> ((input[j] <span style=\"color:#687687\">==<\/span> input[j].<span style=\"color:#3c4c72;font-weight:700\">toLowerCase<\/span>()) <span style=\"color:#687687\">&amp;<\/span><span style=\"color:#687687\">&amp;<\/span> (input[j] <span style=\"color:#687687\">!<\/span><span style=\"color:#687687\">=<\/span> input[j].<span style=\"color:#3c4c72;font-weight:700\">toUpperCase<\/span>())) {\n        possible <span style=\"color:#687687\">=<\/span> <span style=\"color:#d80800\">\"abcdefghijklmnopqrstuvwxyz\"<\/span>;\n    } <span style=\"color:#0100b6;font-weight:700\">else<\/span> <span style=\"color:#0100b6;font-weight:700\">if<\/span> (<span style=\"color:#d80800\">'0123456789'<\/span>.<span style=\"color:#3c4c72;font-weight:700\">indexOf<\/span>(input[j]) <span style=\"color:#687687\">!<\/span><span style=\"color:#687687\">==<\/span> <span style=\"color:#687687\">-<\/span><span style=\"color:#cd0000;font-style:italic\">1<\/span>) {\n        possible <span style=\"color:#687687\">=<\/span> <span style=\"color:#d80800\">\"0123456789\"<\/span>;\n    } <span style=\"color:#0100b6;font-weight:700\">else<\/span> {\n        possible <span style=\"color:#687687\">=<\/span> <span style=\"color:#d80800\">\"#!@~$%^&amp;*)-_\"<\/span>\n    }\n    text <span style=\"color:#687687\">+<\/span><span style=\"color:#687687\">=<\/span> possible.<span style=\"color:#3c4c72;font-weight:700\">charAt<\/span>(<span style=\"color:#6d79de;font-weight:700\">Math<\/span>.<span style=\"color:#3c4c72;font-weight:700\">floor<\/span>(<span style=\"color:#6d79de;font-weight:700\">Math<\/span>.<span style=\"color:#3c4c72;font-weight:700\">random<\/span>() <span style=\"color:#687687\">*<\/span> possible.<span style=\"color:#06960e;font-weight:700\">length<\/span>));\n    }\n  <span style=\"color:#0100b6;font-weight:700\">return<\/span> text;\n}\n<\/pre>\n<h2>Live Demo<\/h2>\n<p>The example below generates ten strings that match this pattern: <strong>AA aaa1111 ### Aaaaaa<\/strong><br \/>\nSwitch to the Result tab to see the output:<br \/>\n<script async=\"\" src=\"\/\/jsfiddle.net\/pdn9s7y9\/2\/embed\/js,html,result\/\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;m going to present a JavaScript code that generates a random string in a certain pattern. The JS function takes a string that matches the given pattern and returns a random string that contains the same type of characters in the same order as the sample.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-609","post","type-post","status-publish","format-standard","hentry","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Random string pattern generator in JavaScript function<\/title>\n<meta name=\"description\" content=\"JavaScript code that generates a random string in a certain pattern. The JS function takes a string that matches the given pattern and returns a random\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Random string pattern generator in JavaScript function\" \/>\n<meta property=\"og:description\" content=\"JavaScript code that generates a random string in a certain pattern. The JS function takes a string that matches the given pattern and returns a random\" \/>\n<meta property=\"og:url\" content=\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"HTML Online\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/htmlcoding\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-11T18:39:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-20T11:57:02+00:00\" \/>\n<meta name=\"author\" content=\"HTML Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"HTML Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/\"},\"author\":{\"name\":\"HTML Editor\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\"},\"headline\":\"Random string pattern generator in JavaScript\",\"datePublished\":\"2018-03-11T18:39:34+00:00\",\"dateModified\":\"2023-12-20T11:57:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/\"},\"wordCount\":167,\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/\",\"url\":\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/\",\"name\":\"Random string pattern generator in JavaScript function\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/#website\"},\"datePublished\":\"2018-03-11T18:39:34+00:00\",\"dateModified\":\"2023-12-20T11:57:02+00:00\",\"description\":\"JavaScript code that generates a random string in a certain pattern. The JS function takes a string that matches the given pattern and returns a random\",\"breadcrumb\":{\"@id\":\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/html-online.com\/articles\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Random string pattern generator in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/html-online.com\/articles\/#website\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"name\":\"HTML Online Articles\",\"description\":\"Tips, tricks, tutorials\u2026\",\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/html-online.com\/articles\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/html-online.com\/articles\/#organization\",\"name\":\"HTML Online\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"contentUrl\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"width\":350,\"height\":350,\"caption\":\"HTML Online\"},\"image\":{\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/htmlcoding\/\",\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\",\"name\":\"HTML Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"caption\":\"HTML Editor\"},\"description\":\"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/@htmlg\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Random string pattern generator in JavaScript function","description":"JavaScript code that generates a random string in a certain pattern. The JS function takes a string that matches the given pattern and returns a random","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/","og_locale":"en_GB","og_type":"article","og_title":"Random string pattern generator in JavaScript function","og_description":"JavaScript code that generates a random string in a certain pattern. The JS function takes a string that matches the given pattern and returns a random","og_url":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/","og_site_name":"HTML Online","article_publisher":"https:\/\/www.facebook.com\/htmlcoding\/","article_published_time":"2018-03-11T18:39:34+00:00","article_modified_time":"2023-12-20T11:57:02+00:00","author":"HTML Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"HTML Editor","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/#article","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/"},"author":{"name":"HTML Editor","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d"},"headline":"Random string pattern generator in JavaScript","datePublished":"2018-03-11T18:39:34+00:00","dateModified":"2023-12-20T11:57:02+00:00","mainEntityOfPage":{"@id":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/"},"wordCount":167,"publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"articleSection":["JavaScript"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/","url":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/","name":"Random string pattern generator in JavaScript function","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/#website"},"datePublished":"2018-03-11T18:39:34+00:00","dateModified":"2023-12-20T11:57:02+00:00","description":"JavaScript code that generates a random string in a certain pattern. The JS function takes a string that matches the given pattern and returns a random","breadcrumb":{"@id":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/html-online.com\/articles\/random-string-pattern-generator-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/html-online.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Random string pattern generator in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/html-online.com\/articles\/#website","url":"https:\/\/html-online.com\/articles\/","name":"HTML Online Articles","description":"Tips, tricks, tutorials\u2026","publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/html-online.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/html-online.com\/articles\/#organization","name":"HTML Online","url":"https:\/\/html-online.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","contentUrl":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","width":350,"height":350,"caption":"HTML Online"},"image":{"@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/htmlcoding\/","https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww"]},{"@type":"Person","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d","name":"HTML Editor","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","caption":"HTML Editor"},"description":"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.","sameAs":["https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/@htmlg"]}]}},"_links":{"self":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/comments?post=609"}],"version-history":[{"count":3,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/609\/revisions"}],"predecessor-version":[{"id":1987,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/609\/revisions\/1987"}],"wp:attachment":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/media?parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/categories?post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/tags?post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}