Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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替换字符串中的#和\s_Javascript_Regex - Fatal编程技术网

使用JavaScript替换字符串中的#和\s

使用JavaScript替换字符串中的#和\s,javascript,regex,Javascript,Regex,如果字符串包含这两个字符,我将尝试替换字符串中的#和空格 这是为降价预览 var t = document.getElementById("textbox"); var h1 = (t.value === "/#\s/") ? t.value.replace(/^[#\s]/, "") : t.value; console.log(h1); 如何解决此问题?如果您想分类删除所有磅标志和空格,则应使用: //var t=document.getElementById(“文本框”); var

如果字符串包含这两个字符,我将尝试替换字符串中的#和空格

这是为降价预览

var t = document.getElementById("textbox");

var h1 = (t.value === "/#\s/") ? t.value.replace(/^[#\s]/, "") : t.value;
console.log(h1);

如何解决此问题?

如果您想分类删除所有磅标志和空格,则应使用:

//var t=document.getElementById(“文本框”);
var t=“你好#世界再见”;
t=t.替换(/[#]/g,”);
控制台日志(t)试试看

let h1=textbox.value.replace(/#|/g',);
控制台日志(h1)
t.value==“/#\s/”
仅当t.value是字面上且准确的
/#s/
时才为真