Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从字符串中剥离一些字符_Javascript_Regex_String - Fatal编程技术网

Javascript 从字符串中剥离一些字符

Javascript 从字符串中剥离一些字符,javascript,regex,string,Javascript,Regex,String,我试图从字符串中删除一些不安全的字符,但我相信我的RegExp对象有问题 下面我尝试做的是,如果有编码长度大于3个字符的字符,则应该用空格替换它们 val = "Angelina’s"; valEnc = encodeURIComponent(val); for(var i = 0; i < val.length; i++){ var a = val.substr(i,1); if(encodeURIComponent(a).length > 3){

我试图从字符串中删除一些不安全的字符,但我相信我的RegExp对象有问题

下面我尝试做的是,如果有编码长度大于3个字符的字符,则应该用空格替换它们

val = "Angelina’s";
valEnc = encodeURIComponent(val);

for(var i = 0; i < val.length; i++){
    var a = val.substr(i,1);
    if(encodeURIComponent(a).length > 3){
        console.log(a, encodeURIComponent(a));
        var re = new RegExp(encodeURIComponent(a),"ig");
        valEnc.replace(re," ");
    };
};

console.log(decodeURIComponent(valEnc));
所以,如果编码的值是
%3D
,这是
=
符号,则可以在我的字符串中包含。但是如果它是一个
撇号
%E2%80%99
则应替换为空格

val = "Angelina’s";
valEnc = encodeURIComponent(val);

for(var i = 0; i < val.length; i++){
    var a = val.substr(i,1);
    if(encodeURIComponent(a).length > 3){
        console.log(a, encodeURIComponent(a));
        var re = new RegExp(encodeURIComponent(a),"ig");
        valEnc.replace(re," ");
    };
};

console.log(decodeURIComponent(valEnc));
val=“安吉丽娜的”;
价=组分(val);
对于(变量i=0;i3){
日志(a,组件(a));
var re=新的RegExp(编码组分(a),“ig”);
价。替换(重“”);
};
};
console.log(解码组件(价));

这段代码可以工作并记录我不需要的字符,但它不能用空格代替它们,我做错了什么?谢谢。

您在这里似乎不必要地使用正则表达式。一种方法是一次生成一个字符的结果字符串:

val = "Angelina’s";
valEnc = "";

for(var i = 0; i < val.length; i++){
    var a = val.substr(i,1);
    var e = encodeURIComponent(a);
    if(e.length <= 3){
        valEnc += e;
    }
}

console.log(decodeURIComponent(valEnc));
val=“安吉丽娜的”;
价=”;
对于(变量i=0;iif(e.length“So if encryted value…”是“encoded”@NullUserException午夜错误,谢谢修复…完全正确,我最好睡一觉,继续用新大脑。谢谢老兄。。。