Vue js Replace spaces And Special Character with dashes and make all letters lower-case
toLowerCase()
method. Then, use a regular expression /[^a-z0-9]+/g
to match all non-alphanumeric characters and spaces and replace them with a dash (-). The g
flag is used to perform a global search and replace.In this tutorial we will learn how to generate Vue js Url Slug using Javascript regular expressionThanks for your feedback!
Your contributions will help us to improve service.
In Vue.js, convert spaces and special characters to dashes and change all letters to lowercase Synatax
How to Generate Vue Js url slug?
This code could be used in a Vue.js component to generate a URL slug based on user input. For example, if a user entered "My Awesome Blog Post!" as the title of a blog post, this code could be used to generate the URL slug "my-awesome-blog-post".
This code takes a string value and performs two operations on it:
-
.toLowerCase()
: This function converts all alphabetic characters in the string to lowercase. This is useful for creating a standardized format for URLs. -
.replace(/[^a-zA-Z0-9]+/g, "-")
: This function uses a regular expression to replace all non-alphanumeric characters (denoted by[^a-zA-Z0-9]
) with a dash (-
). Theg
flag indicates that this replacement should occur globally, i.e. for all instances of non-alphanumeric characters in the string.
Together, these two functions ensure that the resulting value of URL
is a lowercase string with all non-alphanumeric characters replaced by a dash.