如何查看“”之间的代码

时间:2013-11-06 23:21:43

标签: javascript

我如何检查此字符串的内容?我知道关键是使用括号,我只是不知道怎么做?

match("abcdef","@C2D!")

我已经有了这个代码我想要删除用户输入函数并替换它以便它查看字符串并确定它是否只包含文本字符a,b,c,d,e,f以及它是否它是否返回true,如果不返回false

var userinput = prompt('Input characters:');

var lowercase = userinput.toLowerCase();

var allowedcharacters = ["a", "b", "c", "d", "e", "f"]

function checkWord(sInput, arAllowedWrds) {
    for (var i = 0; i < arAllowedWrds.length; i++) {
        if (sInput.indexOf(arAllowedWrds[i]) == -1) {
            return false;
        }
    }

    return true;
}

if (lowercase == allowedcharacters){
  alert(true);
}
else{
  alert(false);
}

1 个答案:

答案 0 :(得分:0)

是这样的:

<script>
function checkWord(sInput, allowedcharacters) {
  var regex = new RegExp("^[" + allowedcharacters + "]*$", "i");
  return regex.test(sInput);
}

//
// user input must be only contains alphabet a to f,
// both upper and lowercase are permitted.
//
var allowedcharacters = "abcdef";
var userinput = prompt("Please Input characters in '" + allowedcharacters + "':");

if(userinput) {
  if (checkWord(userinput, allowedcharacters)){
    alert("'" + userinput + "' contains only characters in '" + allowedcharacters + "'.");
  }
  else{
    alert("'" + userinput + "' contains characters out of '" + allowedcharacters + "'.");
  }
}
else{
  alert("userinput is null.");
}

</script>

检查用户输入是否限制在[abcdef]范围内。例如,abc,efde,fedca,但不是ypapa,xmama。