Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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 如何在字符串中找到至少1个大写字母?_Javascript_Ecmascript 6 - Fatal编程技术网

Javascript 如何在字符串中找到至少1个大写字母?

Javascript 如何在字符串中找到至少1个大写字母?,javascript,ecmascript-6,Javascript,Ecmascript 6,在es6 js中,我有: good=word=>{ word.split(“”).includes(w=>w==w.toUpperCase()) } log(good('Bla'))如果您也需要它的索引,可以这样做 function findUpperCase(str) { return str.search(/[A-Z]/); } 您可以使用包含所有大写字母的正则表达式[a-Z]: const good=word=>/[A-Z]/.test(word); console.log(

在es6 js中,我有:

good=word=>{
word.split(“”).includes(w=>w==w.toUpperCase())
}

log(good('Bla'))
如果您也需要它的索引,可以这样做

function findUpperCase(str) {
    return str.search(/[A-Z]/);
}
您可以使用包含所有大写字母的正则表达式
[a-Z]

const good=word=>/[A-Z]/.test(word);
console.log(良好('Bla'));

console.log(良好('bla'))尽管有更简单的方法(Tushar注释中的正则表达式就是其中之一),但可以通过以下方式修复您的正常工作尝试:

  • 删除大括号以便函数实际返回值
  • 使用
    .some()
    ,它将函数作为其参数
    .includes()
    没有
  • 添加
    const
    ,这样实际上就是在声明函数
const good=word=>word.split(“”).some(w=>w==w.toUpperCase())
console.log(良好('Bla'))
log(good('bla'))
//将执行测试的字符串
让字符串=‘你好,世界’
//函数查找答案
功能字符串检查(接收字符串){
//从字符串中删除特殊字符、数字和空格以执行精确输出
让stringToTest=receivedString.replace(/[^A-Z]+/ig,”)
//要计数的变量:该字符串中有多少大写字符
设j=0
//循环遍历字符串的每个字符,以确定是否有大写字母可用
对于(i=0;i=1){
返回真值
}否则{
返回错误
}
}
//调用函数
让响应=字符串检查(字符串)
log('响应:'+响应)

/[A-Z]/.test(word)
includes
测试函数本身是否包含在内(因为您刚刚创建了它,所以不会包含在内)。如果您想要找到它的位置的索引,请使用我给出的答案。不完全重复,但您可以将其用作参考:
// The string which will go thorough the test
let theString = 'Hello World'

// Function to find out the answer
function stringCheck (receivedString) {
  // Removing special character, number, spaces from the string to perform exact output
  let stringToTest = receivedString.replace(/[^A-Z]+/ig, "")
  // Variable to count: how many uppercase characters are there in that string 
  let j = 0
  // Loop thorough each character of the string to find out if there is any uppercase available
  for (i = 0; i < stringToTest.length; i++) {
    // Uppercase character check
    if (stringToTest.charAt(i) === stringToTest.charAt(i).toUpperCase()) {
      console.log('Uppercase found: ' + stringToTest.charAt(i))
      j++
    }
  }
  console.log('Number of uppercase character: ' + j)
  // Returning the output
  if (j >= 1) {
    return true
  } else {
    return false
  }
}

// Calling the function
let response = stringCheck(theString)
console.log('The response: ' + response)