2016-05-16 13 views
0

Google Apps Scriptを使用して、データベース内の簡単なコメントエンティティからGoogleスプレッドシートにテキスト文字列を返します。私は[Status:Complete]のような私自身の 'bbCode'を含むコメントを特定したいと思います。しかし、私はどのように '完全な'テキストを抽出するか、コメントテキストから '[Status:Complete]' bbCode全体を削除する方法がわかりません。正規表現を使用して文字列を抽出する

例1コメントテキスト: '[ステータス:完了] LoremのイプサムBLAのBLA'
所望の出力:   のCol1:「LoremのイプサムBLAのBLAを任意の提案をありがとう - 私がこれまでに持っているところであります'    Col2に: 'コンプリート'

例2コメントテキスト: 'Loremの[ステータス:提案]イプサムBLAのBLA'
所望の出力:   のCol1: 'LoremのイプサムBLAのBLA'     Col2に:' 提案'

例3コメントテキスト: 'LoremのイプサムBLAのBLA'
所望の出力:   のCol1: 'Loremのイプサム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

'
'と 'nbsp;'は実際のコメントテキストの一部ですか、あるいは書式設定のものですか? – Laurel

+0

こんにちはローレル、入力コメント文字列にはいくつかのhtmlが含まれているかもしれませんが、この例ではいくつかの奇妙な書式設定があると思います。 – user2798561

答えて

0

は、このサンプルをお試しください:

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('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'); 

} 
関連する問題