简体   繁体   中英

Google Sheets Script: Getting values between two values?

var mySTR = "some text some text some text1 text some text1 some text, some text some text text1 some other text text2 some text."




// i want as result: all text bewteen last appearing "text1" and "text2"

var result = "text1 some other text text2"

How can i solve this ? Thanks in advance

You can define a function that just looks for the position of the two delimiters in the string. Than you can extract the text in between those two positions.

You can use the string.lastIndexOf() function to find the last match. Once you have the two indices, you can use the string.splice() function to extract the part out of the string.

A quick fiddle to show what I mean:
https://jsfiddle.net/Daniel_I_Am/5aq4tucp/28/

mySTR.slice(mySTR.lastIndexOf('text1')+5,mySTR.lastIndexOf('text2'));

function textbetween(ss1,ss2,s) {
  var ss1=ss1||'text1';
  var ss2=ss2||'text2';
  var s=s||"some text some text some text1 text some text1 some text, some text some text text1 some other text text2 some text."
  Logger.log(s.slice(s.lastIndexOf(ss1)+ss1.length,s.lastIndexOf(ss2)));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM