regex - RegExp javascript : authorize slash -
i have regexp format url in html input. want remove character not letters or numbers.
so write regexp :
return url.tolowercase() .replace(/^\s+|\s+$/g, "") .replace(/[_|\s]+/g, "-") .replace(/[^a-z\u0400-\u04ff0-9-]+/g, "") .replace(/[-]+/g, "-") .replace(/^-+|-+$/g, "") .replace(/[-]+/g, "-");
but now, want accept slash character ( / ). how can accept character through replaces ? not sure regexp.
i want string :
category / test name = dog
to become :
category/test-name-dog
if want follow logic, need "protect" /
symbol in negated character class (here - /[^a-z\/\u0400-\u04ff0-9-]+/g
- not remove early), , replace -/-
/
final step. note duplicating .replace(/[-]+/g, "-")
step, can remove first one.
return url.tolowercase() .replace(/^\s+|\s+$/g, "") .replace(/[_\s]+/g, "-") .replace(/[^a-z\/\u0400-\u04ff0-9-]+/g, "") .replace(/^-+|-+$/g, "") .replace(/-+/g, "-") .replace(/-*\/-*/g, "/");
url = "category / test name = dog"; document.body.innerhtml = "old: " + url.tolowercase() .replace(/^\s+|\s+$/g, "") .replace(/[_|\s]+/g, "-") .replace(/[^a-z\u0400-\u04ff0-9-]+/g, "") .replace(/[-]+/g, "-") .replace(/^-+|-+$/g, "") .replace(/[-]+/g, "-"); // , document.body.innerhtml += "<br/>new: " + url.tolowercase() .replace(/^\s+|\s+$/g, "") .replace(/[_\s]+/g, "-") .replace(/[^a-z\/\u0400-\u04ff0-9-]+/g, "") .replace(/^-+|-+$/g, "") .replace(/-+/g, "-") .replace(/-*\/-*/g, "/");
Comments
Post a Comment