如何在 InDesign GREP 中使用 javascript 查找/更改将 $2 值存储到变量 [英] How to store $2 value to variable in InDesign GREP find / change using javascript

查看:19
本文介绍了如何在 InDesign GREP 中使用 javascript 查找/更改将 $2 值存储到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 InDesign javascript,它会执行 GREP 查找/更改 - 实际上现在我只需要找到一些文本并将 grep 在 $2 值中找到的内容保存到我的脚本变量中 - 我知道脚本的所有其他内容 -所以我现在需要的就是找出如何获得这 2 美元

I'm writing InDesign javascript, that do GREP find / change - actually for now I need only find some text and save to my script variable what grep found in $2 value - everything else for my script I know how to do - so all I need for now is to find out how to get this $2

简单例子:

app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(\\d)+(\\d)";
found = app.activeDocument.findGrep();
...
found[0].contents; // this store entire string with both digits and plus, and I need only $2 value (second digit in this case)

推荐答案

InDesign 的 JS 界面不会为您做这些,它只返回完整的匹配.

InDesign's JS interface does not do that for you, it only returns the complete match.

由于 contents 是一个简单的 Javascript 字符串(不再是原生的 InDesign 文本),您可以使用 Javascript 自己的 match 函数:

Since contents is a simple Javascript string (not native InDesign text anymore), you can use Javascript's own match function:

..
app.findGrepPreferences.findWhat = "(\\d)+(\\d)";
found = app.activeDocument.findGrep();
m = found[0].contents.match (/(\d)+(\d)/);
alert (m.join('\n'));

注意不要将 InDesign 的 GREP 语法与 Javascript 的语法混合使用.特别是~<等特殊字符是ID扩展,在JS中会失效.

Be careful not to mix InDesign's GREP syntax with Javascript's. In particular, special characters such as ~< are ID extensions and will fail in JS.

请注意,对于2014"的输入,这将返回

Note that for an input of "2014" this will return

2014
1
4

其中第一行是完整匹配(等于 $0),1$14$2.这很可能不是您所期望的.由于您反复将组 1"与 + 匹配,因此每个下一个单个数字都会替换最后一个找到的数字(期待最后一个).你的意思可能是

where the first line is the full match (equal to $0), 1 is $1 and 4 is $2. This is most likely not what you expected. Since you are repeatedly matching "group 1" with the +, each next single digit replaces the last found one (expect for the very last one). You probably meant something like

(\d+)(\d)

哪个会返回

2014
201
4

这篇关于如何在 InDesign GREP 中使用 javascript 查找/更改将 $2 值存储到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆