eds*_*cle -2 html javascript encode space
你好,你可以给我一个javascript函数来替换空格
我用Google搜索,无法让他们工作.我目前正在使用此功能:
function escapeHTMLEncode(str)
{
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
Run Code Online (Sandbox Code Playgroud)
查看正则表达式:
return str.replace(/\s+/g, ' ');
Run Code Online (Sandbox Code Playgroud)
但是,您的函数名称escapeHTMLEncode表明您想要做的不仅仅是替换空格.你能澄清一下你的问题吗?另请参阅在Javascript中将特殊字符转换为HTML,这似乎是您要做的事情.
请注意,该\s+模式将匹配任何连续的空格序列.如果你想只替换空格字符(), and replace each of them with ,使用
return str.replace(/ /g, ' ');
Run Code Online (Sandbox Code Playgroud)