Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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 移除&;nbsp;如果超过10个_Javascript_Html_Regex_Ckeditor - Fatal编程技术网

Javascript 移除&;nbsp;如果超过10个

Javascript 移除&;nbsp;如果超过10个,javascript,html,regex,ckeditor,Javascript,Html,Regex,Ckeditor,我想在任意两个单词之间只允许最多10个,并删除剩余的。如何使用正则表达式在JavaScript中实现这一点?或者: str.replace(/\ {11,}/g, " "); str.replace(/(\ {10})\ */g, "$1") 对于这个要求,您不必使用正则表达式。我们将在一个简单函数中使用JavaScript字符串对象的方法,如下所示: function firstTen(txt){ arr = txt.split(" "); out = ''; for (

我想在任意两个单词之间只允许最多10个
,并删除剩余的
。如何使用正则表达式在JavaScript中实现这一点?

或者:

str.replace(/\ {11,}/g, "          ");
str.replace(/(\ {10})\ */g, "$1")

对于这个要求,您不必使用正则表达式。我们将在一个简单函数中使用JavaScript字符串对象的方法,如下所示:

function firstTen(txt){
arr = txt.split(" ");
out = '';
for (i = 0; i < arr.length; i++){
if (i < 10){
out += arr[i]+" ";
}
else{
out += arr[i];
}
}
    return out;
}
txt = "1 2 3 4 5 6 7 8 9 10 Apple Egypt Africa"
    alert(firstTen(txt));​
函数firstTen(txt){
arr=txt.split(“”);
out='';
对于(i=0;i

下面是一个演示:

我首先创建一个带有10个
的变量

for (var spaces = '', i = 0; i < 10; i++) spaces += '&nbsp;';
下面是模式的分解:

([^\s])?          # 0 or 1 character other than white space
(\s|&nbsp;){11,}  # any white space or &nbsp; used more than 10
(?=[^\s]|$)       # followed by a character other than a white space
                  # or it is the end of string

编辑:我替换了模式中的单词边界字符(
\b
),因为它与unicode字符边界不匹配。

最简单的方法可能是删除所有空格(如果超过10个),然后如果我们假设您只需要2个空格,则添加10“这是WorldiHaveBeenSen中最重要的两个空格”前面的引文是否说明了您想要什么?对不起,您是想同时匹配``和
,还是仅仅使用来避免混淆(!)?
str.replace(/({10})+/g,$1')
,但这既不是
也不是ckeditor。
([^\s])?          # 0 or 1 character other than white space
(\s|&nbsp;){11,}  # any white space or &nbsp; used more than 10
(?=[^\s]|$)       # followed by a character other than a white space
                  # or it is the end of string