Question
Fill in the urlSlug function so it converts a string title and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don't use replace. Here are the requirements:
The input is a string with spaces and title-cased words
The output is a string with the spaces between words replaced by a hyphen (-)
The output should be all lower-cased letters
The output should not have any spaces.
My Solution
var globalTitle = " Winter Is Coming";
function urlSlug(title) {
let regex = /(?<!\s)\s(?!\s)/g
let a = title
.toLowerCase()
.trim()
.split(regex)
.join('-')
return a;
}
console.log(urlSlug(globalTitle))
My Question
I want to use positive and negative look aheads / look behinds to resolve this problem: my particular issue seems to be if the string has more than one space. What changes can be made to make this work?
You can use quantifier +
which means one or more
var globalTitle = " Winter Is Coming";
function urlSlug(title) {
let regex = /\s+/g
let a = title
.toLowerCase()
.trim()
.split(regex)
.join('-')
return a;
}
console.log(urlSlug(globalTitle))