I have such string with requirements to exclude anything except
a-zA-Z0-9,
special characters
()+-.,‘?/:
Also double or more slashes should be restricted And string should not start and end with slash.
Example:
var str = "///a/ab*/*/bc:dD:123a///'Ad/,.?/!//";
//if I use js replace with regex rule twice I get needed result
"///a/ab*/*/bc:dD:123a///'Ad,.?/!//"
.replace(/[^0-9a-zA-Z()+-.,‘?/:]/g, "")
.replace(/[^\/+|\/+$|\/{2,}]/g, "");
//result
"a/abbc:dD:123aAd/,.?"
**Is it possible to combine these rules into one regex rule?!**
//tried to combine these rules by '|' but get failure result
"///a/ab*/*/bc:dD:123a///'Ad/,.?/!//"
.replace(/([^0-9a-zA-Z()+-.,‘?/:])|^\/+|\/+$|\/{2,}/g, "")
//result
"a/ab//bc:dD:123aAd/,.?/"
.replace(/^\/+|\/+$|\/{2,}|[^0-9a-zA-Z()+.,‘?\/:-]+/g, "")*removal, they were not consecutive in the first place..replace(/^\/+|(^|[^\/])\/(?:[^0-9a-zA-Z()+.,‘?\/:-]+\/)*\/+$|\/{2,}|\/(?:[^0-9a-zA-Z()+.,‘?\/:-]+\/(?!\/))+|[^0-9a-zA-Z()+.,‘?\/:-]+/g, "$1"). I get"a/abbc:dD:123aAd/,.?"