“Konversi ke slug JavaScript” Kode Jawaban

Java Script mengubah teks menjadi siput

function convertToSlug(Text)
{
    return Text
        .toLowerCase()
        .replace(/ /g,'-')
        .replace(/[^\w-]+/g,'')
        ;
}
Muddy Mockingbird

Buat Slug di JavaScript

function makeSlug(slug){
  let finalSlug = slug.replace(/[^a-zA-Z0-9]/g, ' ');
  //remove multiple space to single
  finalSlug = slug.replace(/  +/g, ' ');
  // remove all white spaces single or multiple spaces
  finalSlug = slug.replace(/\s/g, '-').toLowerCase().replace(/[^\w-]+/g, '-');
  return finalSlug;
}

//example of work 
let slug = makeSlug('What Is a CSS Framework? (And When to Use 6 Popular Options)           ') 

console.log(slug) // what-is-a-css-framework---and-when-to-use-6-popular-options------------
Sharifur Robin

Konversi ke slug JavaScript

    Method 1: 
    var slug = (req.body.package_name).toString().toLowerCase().replace(/\s+/g, '-').replace(/[^\u0100-\uFFFF\w\-]/g,'-').replace(/\-\-+/g, '-').replace(/^-+/, '').replace(/-+$/, '');
    console.log(slug);
    
    Text Tested : "What Is a CSS Framework? (And When to Use 6 Popular Options)           "
    Output : what-is-a-css-framework-and-when-to-use-6-popular-options
    
    Method 2 with function: 
   			 function slugify(text){
  			 return text.toString().toLowerCase()
    		.replace(/\s+/g, '-')           // Replace spaces with -
    		.replace(/[^\u0100-\uFFFF\w\-]/g,'-') // Remove all non-word chars ( fix for UTF-8 chars )
    		.replace(/\-\-+/g, '-')         // Replace multiple - with single -
    		.replace(/^-+/, '')             // Trim - from start of text
    		.replace(/-+$/, '');            // Trim - from end of text
									}
 var slug = slugify(Text);
console.log(slug);
Beautiful Buzzard

Jawaban yang mirip dengan “Konversi ke slug JavaScript”

Pertanyaan yang mirip dengan “Konversi ke slug JavaScript”

Lebih banyak jawaban terkait untuk “Konversi ke slug JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya