0

This is a chunk of Google Apps Script code:

var re = /(<.*?>)+/;
var strip = str.replace(re, "");
Logger.log(strip);

Why does it strip only the first instance of tag?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Andrew Anderson
  • 1,044
  • 3
  • 17
  • 26
  • This is a oft repeated question on stack overflow that in all cases is trivial to find. Knowing that Google apps script is JavaScript would have helped find all these previous solutions. One of which is: – JSDBroughton Nov 01 '15 at 01:42
  • While I acknowledge that the solution to my answer can be found in this question: [How to replace multiple strings with the .replace() Method](http://stackoverflow.com/questions/14013223/how-to-replace-multiple-strings-with-the-replace-method), I, however, think my question is more Google Script oriented. – Andrew Anderson Nov 01 '15 at 19:52
  • Yes and no. Google Apps Script is by and large a runtime for JavaScript. The Google specific part is in the APIs that reference Google services. Regex is baked in JavaScript functionality. Apps Script doesn't bring anything to that party. – JSDBroughton Nov 02 '15 at 07:53

1 Answers1

3
var re = /(<.*?>)/g

The trailing g is a flag you need to set to replace all matching instances. Depending on the content of str you are passing Another flag you may wish to try adding is m which signifies that the pattern should apply to multiple lines i.e.

var re = /(<.*?>)/mg
JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
  • Thanks! It works with `/g`. Where can I find documentation about it? Because those regex testers I used, work in the way I need without a `/g`. – Andrew Anderson Nov 01 '15 at 16:58
  • Depends how badly/well implemented those interpreters are. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Parameters – JSDBroughton Nov 02 '15 at 07:55
  • Recently, I've been really happy with RegEx101. [Here is your RexEx](https://regex101.com/r/xE2kA7/1), and [here is version 2](https://regex101.com/r/xE2kA7/2) with Jonathon's suggestion added. – Mogsdad Nov 03 '15 at 19:07
  • I use http://regexpal.com. it looks a little more _web1.0_ but the grouping highlighting has always helped me iteratively construct my queries. particularly when dealing with conditional lookaheads etc. – JSDBroughton Nov 04 '15 at 10:49