【发布时间】:2012-02-19 17:13:28
【问题描述】:
我写了一些正则表达式。当我使用在控制台中键入的字符串执行它时它可以工作,但在某些情况下在我的脚本中却不行。
这是我的控制台输出:
>body
["VERSION:2.1", "N:;S Wicius;;;", "FN:S Wicius", "TEL;PREF;CELL:000000000"]
>records.line
/^([^:;]+)(?:;([^:]+))?:(.+)$/gm
>records.line.exec( body[1] )
null
>body[1] == "N:;S Wicius;;;"
true
>records.line.exec( "N:;S Wicius;;;" )
["N:;S Wicius;;;", "N", undefined, ";S Wicius;;;"]
>for( var i = 0; i < body.length; i++ ) {
var line = [];
if( line = records.line.exec( body[i] ) )
console.log( line )
}
["VERSION:2.1", "VERSION", undefined, "2.1"]
["FN:S Wicius", "FN", undefined, "S Wicius"]
【问题讨论】:
-
你想做什么?你能解释清楚一点吗?
-
body[1]确实是那个字符串,body不是稀疏数组吗? -
typeof body[1] 产生“字符串”。我想解析它以解析“body”数组中的所有字符串。
-
body是由\n分割的文本。这会导致问题吗?
标签: javascript regex match