[英]Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method
i'm using react native with regex我正在使用带有正则表达式的本机反应
if i use my code如果我使用我的代码
this error occured发生此错误
Invalid attempt to destructure non-iterable instance.对不可迭代实例的解构尝试无效。 In order to be iterable, non-array objects must have a Symbol.iterator method.
为了可迭代,非数组对象必须具有 Symbol.iterator 方法。
I want to put an empty value like ' ' in the match if any character other than a number and decimal point is included in my regular expression.如果我的正则表达式中包含除数字和小数点以外的任何字符,我想在匹配项中放置一个空值,例如 ' '。 If you put "123abc" in the value variable, match returns "123", but if you put "acv" in the value constant, the above error occurs.
如果将“123abc”放入 value 变量中,则 match 返回“123”,但如果将“acv”放入 value 常量中,则会出现上述错误。 In this case, how can I put an empty string into the match without generating an error?
在这种情况下,如何将空字符串放入匹配项而不产生错误?
const regex = /\d+(\.\d{1,2})?/;
const value = "abd"
const [match] = regex.exec(value);
you can use ||
你可以使用
||
operator.操作员。 in case
exec
return null
it will default to empty string ''
.如果
exec
返回null
,它将默认为空字符串''
。
function getStrings(value) { const regex = /\d+(\.\d{1,2})?/; const [match] = [regex.exec(value) || '']; if(Array.isArray(match)) return match[0] return match } console.log(getStrings("abd")) console.log(getStrings("123abd"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.