2017-02-27 59 views
0

我想从下面的字符串中获取行号。我想下面让行中没有,但看起来并不可靠从给出字符串找到行号

string.split('Line')[1].split(':')[0]

字符串:

"Line 30:6 Table not found 'users'"

输出:

{ line: 30, statement: "Table not found 'users'" }

回答

0

var x = "Line 30:6 Table not found 'users'"; 
 
y = x.split(":"); 
 

 
var newObj = { 
 
    line : y[0].split(" ")[1], 
 
    data : y[1].substr(2) 
 
} 
 

 
console.log(newObj);

0

可以使用Regular Expression从一个字符串中提取数据。

实施例:下面的提取物()函数将做的工作

var regex = /^Line ([0-9]+):[0-9]+ (.*)$/g 

function extract(str){ 
    var result = regex.exec(str); 
    if(result != null){ 
     return { 
      "line": result[1], 
      "data": result[2] 
     } 
    }else{ 
     return null; 
    } 
} 


var input = "Line 30:6 Table not found 'users'"; 
var output = extract(input);