I have the following RegEx:
$('.my-selector').each(function(){
var t = $(this).text(),
id = t.toLowerCase().replace(/\s+/g, '-');
id = id.replace(/[^a-zA-Z0-9-]/g, "");
});
This repalces all spaces with a -
and then removes any character that isn't a-z
, 0-9
or -
. This works but I noticed one thing, if I have a trailing space it becomes a -
. For examples. My (test) string
becomes my-test-string-
How to I remove the last -
(or ) from the very end of the string?