Need Help with String Manipulation

I’m quite new to scripting but wrote the following. It won’t even run so any feedback would be appreciated. Loving Drafts 5 so far!

var d = Draft.content();
var sub1 = '## Invited';
var ninv = d.search(sub1);

If you want to work with the current existing draft then Draft should be lowercase. You’d use capital D Draft with create() to make a new one. Welcome to the quirks of javascript.

1 Like

Thanks for the tip. I just tried this. Still not running…

Try this…

var d = draft.content;
var sub1 = "## Invited";
var ninv = d.search(sub1);
if (ninv == -1)
{
	alert("No occurrences of\n'" + sub1 + "'\nwere found.");
}
else
{
	alert("First occurrence of\n'" + sub1 + "'\nis at character position " + ninv + ".");
}
3 Likes

Worked perfectly. Thanks!

1 Like

Just to highlight the changes then…

  • Changed the D to a d as per @dfay.
  • Removed the () from content (attribute not a function).

I also changed the quotes but that was just for consistency with my other lines :wink:

1 Like

Makes sense now. Thanks!