Grep code works inside indesign but not in script

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Grep code works inside indesign but not in script

Viewing 4 reply threads
  • Author
    Posts
    • #85308
      brad wilson
      Member

      Hi all,

      I’ve been using the following GREP in Indesign’s Find/Change and it works fine, but when I try to use it via a Script it doesn’t do anything.

      Find what:
      (\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(\s)
      Change to:
      \t$2\t$4\t$6\t$8\t

      This is what I’m trying to run as part of the script:

      //grep {findWhat:”(\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(\s)”} {changeTo:”\t$2\t$4\t$6\t$8\t”} {}

      Basically i’m just trying to get rid of the commas and get tabs in the spaces between scores that look like this: teamname 1.2, 2.1, 3.3, 4.5 (98)

      I’ve also tried more basic versions just to see if I could change one of the commas to a tab and none of them have worked.

      What am I doing wrong?

      Cheers,
      Brad

    • #85309
      Matt Isaac
      Participant

      I don’t know much about scripts but I do know that the double forward slash creates a comment. so anything following double forward slashes will not be read by the script. Remove those and try again.

    • #85310
      brad wilson
      Member

      Sorry, the comment // wasn’t there when I was trying it out. I can’t edit them out of the original post.

    • #85311
      Ari Singer
      Member

      Whenever you include a backlash within quotes in a script, the script will get confused. For example, if you write: findWhat: "\r" JavaScript will think that you want to insert an actual return in that string (because that’s the way it’s done in JavaScript). So whenever you include a GREP query in a script, any backslashes have to be escaped again. So instead of \r it has to be \\r.

      In other words, for every backslash: one more. (except when you already have two backslashes, such as when you’re searching for an actual backslash, then you have to write three backslashes, not four.

      So in your case, that would be like this:

      {findWhat:”(\\s)(\\d+\\.\\d+)(,\\s)(\\d+\\.\\d+)(,\\s)(\\d+\\.\\d+)(,\\s)(\\d+\\.\\d+)(\\s)”} {changeTo:”\\t$2\\t$4\\t$6\\t$8\\t”} {}

    • #85416
      Ari Singer
      Member

      I think I’ve made a mistake when implying that when using an escaped backslash in a script you need three backslashes not four. As it turns out in my recent experience you actually do need four! And three results in an error.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.
>
Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo