我正在使用Google Apps脚本将文本字符串从数据库中的简单评论实体返回到Google表格。我想识别包含我自己的'bbCode'的某些注释,例如[Status:Complete]。但我不确定如何提取“完整”文本以及如何从注释文本中删除整个“[Status:Complete]”bbCode。这是我到目前为止所要做的 - 谢谢你的任何建议:
示例1评论文本:'[状态:完成] Lorem ipsum bla bla'
期望的输出:Col1:'Lorem ipsum bla bla'Col2:'完成'
示例2评论文本:'Lorem [状态:建议] ipsum bla bla'
期望的输出:Col1:'Lorem ipsum bla bla'Col2:'建议'
示例3评论文本:'Lorem ipsum bla bla'
期望的输出:Col1:'Lorem ipsum bla bla'Col2:
var bbCode = '[Status: ";
//get data from db and for next into val
var val = rs.getString(col+1);
if (val.indexOf(bbCode) > -1) {
// Item with Status, place the status in the Status Column
//but this next line is not right - I would like var Status = 'Complete' or 'Proposed' etc...
var Status = RegExp(.*\[Status: (.*)\].*', 'g');
cell.offset(row, col+2).setValue(Status);
// Place the remaining comment in the Comment column
//This next line is not right I would like val = the comment string without the '[Status: Completed]' or '[Status: Proposed]' etc...
cell.offset(row, col+2).setValue(val);
} else {
// Enter comment in Comment column as normal
cell.offset(row, col+1).setValue(val);
}
答案 0 :(得分:0)
Try this sample:
function RegexGetTwoStrings() {
Logger.log('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^');
var sample = ['[Status: Complete] Lorem ipsum bla bla',
'Lorem [Status: Proposed] ipsum bla bla',
'Lorem ipsum bla bla'];
var RegEx = /(.*)\[.*: (.*)\] (.*)/;
var Replace = "$1$3,$2";
var str, newStr, Col1, Col2;
var strArr = [];
for (var i = 0; i < sample.length; i++) {
str = sample[i];
newStr = str.replace(RegEx, Replace) + ",";
strArr = newStr.split(',');
Col1 = strArr[0];
Col2 = strArr[1];
Logger.log("Sample1 = '" + str + "'");
Logger.log('Col1 = ' + Col1);
Logger.log('Col2 = ' + Col2);
}
Logger.log('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^');
}