Advertisement
MizunoBrasil

Gerador de Slug em Javascript

Jan 25th, 2023 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <title>Gerador de Slug © 2023 Mizuno</title>
  3.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  4.   <body>
  5.       <div class="container">
  6.           <br><br><br>
  7.     <form>
  8.         <div class="form-group">
  9.       <label for="text_field">Informe o texto:</label>
  10.       <input type="text" class="form-control" id="text_field" name="text_field">
  11.       </div>
  12.       <button type="button" onclick="generateSlug()" class="btn btn-primary">Gerar Slug</button>
  13.     </form>
  14.     <div id="output"></div>
  15.     <script>
  16.       function removeSpecialCharacters(text) {
  17.         // Removing accents and cedillas
  18.         var normalizedText = text.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
  19.         // Removing single and double quotes
  20.         var withoutSpecialChars = normalizedText.replace(/[\"\']/g, "");
  21.         return withoutSpecialChars;
  22.       }
  23.  
  24.       function generateSlug() {
  25.         var text = document.getElementById("text_field").value;
  26.         var slug = removeSpecialCharacters(text).toLowerCase().replace(/ /g,"-").replace(/[^\w-]+/g,'');
  27.         document.getElementById("output").innerHTML = slug;
  28.       }
  29.     </script>
  30.   </body>
  31. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement