Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Backslash - Fatal编程技术网

Javascript 如何替换数组中的字符串

Javascript 如何替换数组中的字符串,javascript,regex,string,backslash,Javascript,Regex,String,Backslash,我正在编写一段代码,它使用正则表达式来查找/替换聊天中的表情符号。但是,我希望使用相同的值数组并将它们作为引用输出 正则表达式可以很好地用于我的搜索,但是当我在输出正则表达式搜索字符串以获取帮助之前尝试对其进行替换时,结果仍然是一个斜杠 :\) :\( var emotes = []; emotes[0] = new Array(':\\\)', 'happy.png'); emotes[1] = new Array(':\\\(', 'sad.png'); function listEmo

我正在编写一段代码,它使用正则表达式来查找/替换聊天中的表情符号。但是,我希望使用相同的值数组并将它们作为引用输出

正则表达式可以很好地用于我的搜索,但是当我在输出正则表达式搜索字符串以获取帮助之前尝试对其进行替换时,结果仍然是一个斜杠

:\)
:\(

var emotes = [];
emotes[0] = new Array(':\\\)', 'happy.png');
emotes[1] = new Array(':\\\(', 'sad.png');

function listEmotes(){
    var emotestext = '';
    for(var i = 0; i < emotes.length; i++){

        //Tried this and it doesn't seem to work
        //var emote = emotes[i][0];
        //emote.replace('\\', '');

        emotestext += '<ul>' + emote + ' <img src="emotes/' + emotes[i][1] + '"></ul>';
    }

    return emotestext;
}
:\)
:\(
var-emotes=[];
emotes[0]=新数组(“:\\”,“happy.png”);
emotes[1]=新数组(':\\\(','sad.png');
函数listEmotes(){
var emotestext='';
对于(变量i=0;i';
}
返回emotext;
}

您的问题是
str.replace
不会更改原始变量,而是返回一个新变量。请尝试以下操作:

var emotes = [
    [':\\\)', 'happy.png'],
    [':\\\(', 'sad.png']
];

function listEmotes(){
    var emotestext = '';
    for(var i = 0; i < emotes.length; i++){
        var emote = emotes[i][0].replace('\\', ''); // See what I did here?

        emotestext += '<ul>' + emote + ' <img src="emotes/' + emotes[i][1] + '"></ul>';
    }

    return emotestext;
}
var-emotes=[
[':\\)','happy.png'],
[':\\\(','sad.png']
];
函数listEmotes(){
var emotestext='';
对于(变量i=0;i';
}
返回emotext;
}