quick [php function] -> [javascript function] question
if anyone fancies doing me a really quick favour, it would be really appreciated:
static function make_url_safe($z){
$z = strtolower($z);
$z = preg_replace('/[^a-zA-Z0-9\s] /i', ''开发者_Go百科, $z);
$z = str_ireplace(' ', '-', $z);
return $z;
}
what js functions should i be looking at to convert this function to javascript?
var s = 'Abc- sdf%$987234'.toLowerCase();
s.replace(/[^a-z0-9\s]+/g, '').replace(/ /g, '-');
It's not an exact equivalent, because your original function makes little sense: using i
flag after converting string to lower case, using a-zA-Z
with i
flag on lower-case string, random space after the character class, str_ireplace
with space as a first parameter.
Not equivalent, but has the same goal of making strings URL safe:
encodeURI
or encodeURIComponent
Take a look at String and RegExp classes.
function makeURLSafe(z){
return z.toLowerCase().replace(/[^a-z0-9\s]+/g, '').replace(/ /g, '-');
}
精彩评论