Skip to content Skip to sidebar Skip to footer

Cannot Read Property 'length' Of Null In Javascript For Loop

I'm trying to make a markdown editor like Stack Overflow has. I get this error listed in the title if I don't actually type an asterisk AND a http:// containing phrase into the tex

Solution 1:

instead of using match function which returns a null if the string doesnt match the provided regex use the test function which returns a true/false and then if you need it you can use the match function.

basically you are performing null.length which is obviously invalid as the match is returning you a null

or you could just perform a null check before you check for the length and just do the regex matching once.

if(linkify!==null && linkify.length!== 0)
{
  //do smthing
}

Solution 2:

'linkify' is null when there isn't an 'http://' match in the text, so you need to test for that condition:

if(linkify && linkify.length)

Post a Comment for "Cannot Read Property 'length' Of Null In Javascript For Loop"