Home > Net >  search and replace in various brackets, script on google docs
search and replace in various brackets, script on google docs

Time:07-24

The script: enter image description here

After:

enter image description here

Update (changed)

Here is the new function to convert case (AAA) --> (Aaa) for any text inside the brackets:

function fix_cases_for_any_text_in_brackets() {
  var doc   = DocumentApp.getActiveDocument();
  var body  = doc.getBody();
  var words = body.getText().match(/\(. ?\)/g) || [];
  words = [...new Set(words)]; // remove duplicates

  var replaces = words.map(r => ({
    what: '\\('   r.slice(1,-1)   '\\)',
    to: '('   r[1].toUpperCase()   r.slice(2,-1).toLowerCase()   ')',
  }));
  
  replaces.forEach(replace => body.replaceText(replace.what, replace.to));
}

Update 2*

Here is the function to convert all th 'AllCaps' words: AAAA --> Aaaa:

function fix_AllCaps() {
  const doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var words = body.getText().match(/[A-Z]{2,10}/g) || [];
  words = [...new Set(words)]; // remove duplicates

  var replaces = words.map(r => ({
    what: r,
    to: r[0].toUpperCase()   r.slice(1).toLowerCase(),
  }));

  console.log(replaces);
  
  replaces.forEach(replace => body.replaceText(replace.what, replace.to));
}
  • Related