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
Javascript 如何在react中使用if-else语句验证RegExp?_Javascript_Regex_Reactjs_React Native - Fatal编程技术网

Javascript 如何在react中使用if-else语句验证RegExp?

Javascript 如何在react中使用if-else语句验证RegExp?,javascript,regex,reactjs,react-native,Javascript,Regex,Reactjs,React Native,如果用户在react中的文本输入中键入符号,如何提醒用户? 我试过这个 textChange = () => { if (this.state.texts == new RegExp(/[^a-zA-Z\s]/) ) { alert("No symbols allowed") } } 但值得注意的是,当符号类型为时会发出警报,而不是将字符串的相等性与regex对象进行比较,您需要使用方法,该方法根据传递的字符串匹配模式是否返回布尔值 textChange = (

如果用户在react中的文本输入中键入符号,如何提醒用户? 我试过这个

textChange = () => {
   if (this.state.texts == new RegExp(/[^a-zA-Z\s]/) ) {
       alert("No symbols allowed")
   }
}

但值得注意的是,当符号类型为

时会发出警报,而不是将字符串的相等性与regex对象进行比较,您需要使用方法,该方法根据传递的字符串匹配模式是否返回布尔值

textChange = () => {
   if (/[^a-zA-Z\s]/.test(this.state.text) ) {
      alert("No symbols allowed")
   }
}

您不需要将字符串的相等性与regex对象进行比较,而是需要使用方法,该方法根据传递的字符串匹配模式返回布尔值

textChange = () => {
   if (/[^a-zA-Z\s]/.test(this.state.text) ) {
      alert("No symbols allowed")
   }
}
使用测试或匹配方法

textChange==>{ if/[^a-zA-Z\s]/.testthis.state.text{ 警报不允许使用任何符号 } } 或

使用测试或匹配方法

textChange==>{ if/[^a-zA-Z\s]/.testthis.state.text{ 警报不允许使用任何符号 } } 或


您可以使用javascript的文本方法来使用正则表达式进行验证

textChange =  () => {

  const expression = /[^a-zA-Z\s]/;
  var notValid = expression.test(String(this.state.texts));
  // notValid will be true if entered text does'nt match the expression

  if(notValid){
    alert("No symbols allowed");
  }else{
    //Valid value
  }


}

您可以使用javascript的文本方法来使用正则表达式进行验证

textChange =  () => {

  const expression = /[^a-zA-Z\s]/;
  var notValid = expression.test(String(this.state.texts));
  // notValid will be true if entered text does'nt match the expression

  if(notValid){
    alert("No symbols allowed");
  }else{
    //Valid value
  }


}
如果新建RegExp/[^a-zA-Z\s]/.testthis.state.text或/[^a-zA-Z\s]/.testthis.state.textry如果新建RegExp/[^a-zA-Z\s]/.testthis.state.text或/[^a-zA-Z\s]/.testthis.state.text,请尝试