“Konvertieren Sie JavaScript zu Slug” Code-Antworten

Java -Skript konvertieren Text in Slug

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

Slug in JavaScript erstellen

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

Konvertieren Sie JavaScript zu Slug

    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

Ähnliche Antworten wie “Konvertieren Sie JavaScript zu Slug”

Fragen ähnlich wie “Konvertieren Sie JavaScript zu Slug”

Weitere verwandte Antworten zu “Konvertieren Sie JavaScript zu Slug” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen