无法在 javascript for 循环中读取 null 的属性“长度”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10183614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:12:55  来源:igfitidea点击:

Cannot read property 'length' of null in javascript for loop

javascriptjquerynull

提问by tim peterson

I'm trying to make a markdown editor like Stack Overflow has.

我正在尝试制作一个像 Stack Overflow 一样的 Markdown 编辑器。

I get this error listed in the title if I don't actually type an asterisk AND a http:// containing phrase into the textarea. If I only type an asterisk containing phrase, the error refers to this line: if(linkify.length!==0)

如果我实际上没有在 textarea 中输入星号和包含 http:// 的短语,我会在标题中列出这个错误。如果我只输入一个包含短语的星号,错误是指这一行:if(linkify.length!==0)

Here's a jsfiddleto show you what I mean.

这是一个jsfiddle向您展示我的意思。

Here's my HTML:

这是我的 HTML:

<textarea></textarea>
<div></div><button type="button">Markdownify</button>

Here's my JS:

这是我的JS:

var val=$('textarea').val(), boldify = val.match(/\*\*[A-Za-z0-9]+\*\*/gim),
italicify = val.match(/\*[A-Za-z0-9]+\*/gim),linkify = val.match(/http\:\/\/[A-z0-9]+\.[A-z0-9]+/gim);

$(document.body).on('click', 'button', function() {

val=$('textarea').val(), boldify = val.match(/\*\*[A-Za-z0-9]+\*\*/gim),
italicify = val.match(/\*[A-Za-z0-9]+\*/gim),linkify = val.match(/http\:\/\/[A-z0-9]+\.[A-z0-9]+/gim);

if (boldify.length!== 0){
            for (var i=0; i < boldify.length; i++)  {
                var boldMatch= boldify[i],  
                boldReplace = boldMatch.replace(/\*\*[A-z0-9]+\*\*/gim, '<span style="font-weight:bold;color:blue;">'+boldMatch+'</span>'),
                val = val.replace(boldMatch, boldReplace);     
            }
        val = val.replace(/\*\*/gi, "");
}

if(italicify.length!== 0){
    for(var j=0; j < italicify.length; j++){
    var italicMatch= italicify[j],
     italicReplace = italicMatch.replace(/\*[A-z0-9]+\*/gim, '<span style="font-style:italic;color:red;">'+italicMatch+'</span>');
    val = val.replace(italicMatch, italicReplace); 
    }
    val = val.replace(/\*/gi, "");
}

if(linkify.length!== 0){
    for(var k=0; k < linkify.length; k++){
    var linkMatch= linkify[k],
    linkReplace = linkMatch.replace(/http:\/\/[A-z0-9]+\.[A-z0-9]+/gim, '<a href="'+linkMatch+'">'+linkMatch+'</a>');
        val = val.replace(linkMatch, linkReplace);
    }
}

$('div').html(val);
});

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Baz1nga

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

match如果字符串与提供的正则表达式不匹配test,则不使用返回 null 的函数,而是使用返回 true/false的函数,然后如果需要,可以使用该match函数。

basically you are performing null.lengthwhich is obviously invalid as the matchis returning you a null

基本上你正在执行null.length这显然是无效的,因为它match正在返回一个空值

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

或者您可以null在检查长度之前执行检查,然后只进行一次正则表达式匹配。

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

回答by jimw

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

当文本中没有“http://”匹配时,“linkify”为空,因此您需要测试该条件:

if(linkify && linkify.length)