Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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
如何替换任意数量的字符串jquery_Jquery_Html_Regex - Fatal编程技术网

如何替换任意数量的字符串jquery

如何替换任意数量的字符串jquery,jquery,html,regex,Jquery,Html,Regex,我想将一个html字符串中的数字替换为一个空格。但是,它将取代每一个单一的空间,导致许多空间。比如说, htmlValue.replace(/ /gi, " ");//"  " becomes " ". (2spaces) but I want only 1 space 您可以尝试以下方法: /( )+/gm 并由单个 示例代码: 常量正则表达式=/+/gm; const str=`sfdasdf`; 常量subst

我想将一个html字符串中的数字替换为一个空格。但是,它将取代每一个单一的空间,导致许多空间。比如说,

htmlValue.replace(/ /gi, " ");//"  " becomes "  ". (2spaces) but I want only 1 space
您可以尝试以下方法:

/( )+/gm
并由单个

示例代码:

常量正则表达式=/+/gm; const str=`sfdasdf`; 常量subst=`; const result=str.replaceregex,subst; console.logresult 试试这个

htmlValue.replace(new RegExp(' ', 'g'), ' ');

你能给我们一些样品字符串和预期的输出。。。这让我们不必猜测任何东西,尝试htmlValue.replace/+/gi;