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. Please help me out in resolving this issue.
I think the problem is 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.
you could use [...text.match(regex)||[]]
but that's pretty ugly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.