Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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/2/jsf-2/2.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_Whitespace - Fatal编程技术网

使用javascript在多行中匹配空格和非单词字符

使用javascript在多行中匹配空格和非单词字符,javascript,regex,whitespace,Javascript,Regex,Whitespace,我想在多个输入字段中匹配'/s'和'/W'。 以下是我的HTML代码: <div class="center"> <div class="fname"> <label id="fnamelabel">First Name: </label> <input id="fname" placeholder="First Name"></input> </div>

我想在多个输入字段中匹配'/s'和'/W'。 以下是我的HTML代码:

<div class="center">
    <div class="fname">    
        <label id="fnamelabel">First Name: </label>
        <input id="fname" placeholder="First Name"></input>
    </div>
    <div class="lname">
        <label id="lnamelabel">Last Name: </label>
        <input id="lname" placeholder="Last Name"></input>
    </div>
    <div class="button">
        <input id="button" type="button" value="Submit" onclick="validateField()">
    </div>
如何减少“if”循环


谢谢。

如果结果=fnamestring.matchwsrgx->是什么?你想要什么?此代码仅设置从fnamestring.matchwsrgx返回给结果变量的值。您不能。掩盖if不是循环这一事实。每个正则表达式都有一条自定义错误消息。但是您可以将所有正则表达式、输入和相应的错误消息放入一个对象数组中,至少它使扩展其他测试稍微容易一些。@AndrewEvt我的目标是停止输入空格和非单词字符。但是,从我看到的情况来看,我可能最终会使用很多“if”循环。@Jongware我不喜欢索引,但感谢您的输入。好消息是,空格也是非单词字符。因此,使用一个测试和一条消息。
function validateField(){
var wsrgx = /\s/g;
var chrgx = /\W/g;
var fnamestring = document.getElementById("fname").value;
var lnamestring = document.getElementById("lname").value;
var result;
if (result = fnamestring.match(wsrgx)){
       console.log ("There is a whitespace character in the First Name field \nSource: " + fnamestring)
       return false;
       }
if (result = fnamestring.match(chrgx)){
       console.log ("There is a non-word character in the First Name field: \nSource: " + fnamestring);
       return false;
       }
if (result = lnamestring.match(wsrgx)){
       console.log ("There is a whitespace character in the Last Name field \nSource: " + lnamestring)
       return false;
       }
if (result = lnamestring.match(chrgx)){
       console.log ("There is a non-word character in the Last Name field \nSource: " + lnamestring)
       return false;
       }
}