1

我将如何检查这个字符串的内容?我知道重点不是使用括号,我只是不知道该怎么做?

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

我已经有了这段代码,我想删除用户输入函数并替换它,以便它查看字符串并确定它是否仅包含文本字符 a、b、c、d、e、f,如果包含则返回如果不是,则返回 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);
}
4

1 回答 1

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。

于 2013-11-07T00:12:40.960 回答