【发布时间】:2019-07-25 07:15:21
【问题描述】:
尝试在 javascript 和 PHP 中清理相同格式的字符串。
这个 PHP 函数按原样完美运行:
function sanitize($s) {
// This RegEx removes any group of non-alphanumeric or dash
// character and replaces it/them with a dash
return strtolower(preg_replace('/[^a-z0-9_]+/i', '_', $s));
}
以字符串为例:
Test String - 20AS(AE)0121
PHP函数带回来:
test_string_20as_ae_0121
正在尝试将其转换为对应的 javascript:
function sanitizejs(string) {
s = string.replace(/[^a-z0-9_]+/i, '_')
return s;
}
返回
Test_String - 20AS(AE)0121
我在这里错过了什么?
【问题讨论】:
标签: javascript php regex