.test() 方法在反应 jsx 代码中不起作用
.test() method is not working in react jsx code
我正在使用 .test() 方法检查文本框中的输入值是否与我的 React 应用程序中的正则表达式匹配,下面是我的代码
if(!((data.regex_tx).test(value))){
message = <p style={{color:'red',marginTop:-15}}>{data.vld_msg}</p>
}
其中 data.regex = 来自 json 的正则表达式,值 = 来自文本框的输入值和 data.vld_msg= 要显示的来自 json 的验证消息。获取控制台错误“Uncaught TypeError: data.regex_tx.test is not a function(...)”。语法中有任何错误。请帮助
我认为 data.regex_tx
只是一个 string
,因为它来自 JSON。您必须将 string
转换为 RegExp
,然后调用 test
函数。做如下的事情
var expr = new RegExp(data.regex_tx);
if(expr.test(value)) {
// other code
}
我正在使用 .test() 方法检查文本框中的输入值是否与我的 React 应用程序中的正则表达式匹配,下面是我的代码
if(!((data.regex_tx).test(value))){
message = <p style={{color:'red',marginTop:-15}}>{data.vld_msg}</p>
}
其中 data.regex = 来自 json 的正则表达式,值 = 来自文本框的输入值和 data.vld_msg= 要显示的来自 json 的验证消息。获取控制台错误“Uncaught TypeError: data.regex_tx.test is not a function(...)”。语法中有任何错误。请帮助
我认为 data.regex_tx
只是一个 string
,因为它来自 JSON。您必须将 string
转换为 RegExp
,然后调用 test
函数。做如下的事情
var expr = new RegExp(data.regex_tx);
if(expr.test(value)) {
// other code
}