Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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_Character_Whitespace - Fatal编程技术网

Javascript 如何生成一个正则表达式来测试每个单词后是否存在空格?

Javascript 如何生成一个正则表达式来测试每个单词后是否存在空格?,javascript,regex,character,whitespace,Javascript,Regex,Character,Whitespace,每一个单词后面都有一个空格。如果有一个空格(只有1),那么它就是正确的。 我只有/^[a-zA-Z]+$/我在js上使用这个匹配一个单词,然后重复1空格+1个单词 Regex: /^\w+(?: \w+)*$/ 代码: /^\w+(?: \w+)*$/ var re=/^\w+(?:\w+*$/; var str='word1 word2 word3'; 如果(重新测试(str)){ document.write(“'+str+'”匹配“+re); }否则{ 文档。写入(“不匹配”);

每一个单词后面都有一个空格。如果有一个空格(只有1),那么它就是正确的。
我只有
/^[a-zA-Z]+$/
我在js上使用这个匹配一个单词,然后重复
1空格+1个单词

Regex:

/^\w+(?: \w+)*$/

代码:

/^\w+(?: \w+)*$/
var re=/^\w+(?:\w+*$/;
var str='word1 word2 word3';
如果(重新测试(str)){
document.write(“'+str+'”匹配“+re);
}否则{
文档。写入(“不匹配”);

}
此正则表达式匹配字符串中的每个单词(类似正则表达式的单词还包括下划线
'
),后跟2个或更多空格
\w+(?=\s{2,})/g

javascript中的代码片段,用于提取它们:

var regex = /\w+(?=\s{2,})/g; 
var input = 'Word_1 Wrong_Word  test3 wrongWord_2  and so On  !';
var m;

while ((m = regex.exec(input)) !== null) {
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    // The wrong words are in m[0], m[1] and so on...
}

你能提供你想匹配的内容吗?这样我就可以知道你想在这里做什么了。空格在给定的单词后面。单词由英文字母组成(大写或小写)