[英]Javascript: TypeError: Cannot convert undefined or null to object
I'm reading a text file and converting it to JSON format using regex in my react project.It is working fine but when including in my code it is giving TypeError: Cannot convert undefined or null to object There is some problem while performing replace but I am unable to understand the problem. 我正在读取文本文件并将其在我的react项目中使用正则表达式将其转换为JSON格式。它可以正常工作,但是当包含在我的代码中时,它会给出TypeError:无法将undefined或null转换为对象执行替换时存在一些问题,但是我无法理解问题。 Please help me out in resolving this issue.
请帮助我解决此问题。
I think the problem is allText = allText.replace(/\\s{2,}|\\n+/g, " ");
我认为问题是
allText = allText.replace(/\\s{2,}|\\n+/g, " ");
is not getting completed. 还没有完成。
.match(regex)
returns null
if there are no matches and will cause an error when trying to perform the spread operation. 如果没有匹配项,
.match(regex)
将返回null
,并且在尝试执行传播操作时将导致错误。
you could use [...text.match(regex)||[]]
but that's pretty ugly. 您可以使用
[...text.match(regex)||[]]
但这很丑陋。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.