JQuery convert header to slug

I have a PHP script that does the following: it takes a line, for example, "This is a great blog article, No. 1!" and returns the following line: "this-is-a-great -blog-post-1".

I'm not a jQuery specialist, so I ask this question. Does anyone know a jQuery (or Javascript, for that matter) script that will do the exact same thing as my script? Thanks in advance.

Will

+3
source share
3 answers

Theres a plugin for this! :)

+5
source

Or you can write your own version in about 45 seconds:

var str = "This is a Great Blog Post, #1!";
str = str.replace(/[^a-zA-Z0-9\s]/g,"");
str = str.toLowerCase();
str = str.replace(/\s/g,'-');
document.write(str);

// outputs "this-is-a-great-blog-post-1"
+15
source
+2

Source: https://habr.com/ru/post/1733421/


All Articles