使用 for 循环搜索字段数组以查找和替换文本

Use for loop to search an array of fields to find & replace text

我使用 Adob​​e Designer (LiveCycle) 创建了一个 XFA 表单,它使用了 25 个文本字段。我正在尝试创建一个 'Redact' 按钮,当按下该按钮时,它将遍历 25 个字段并在每个字段的字符串值中搜索名称。如果找到该名称,则使用全局正则表达式将名称替换为“********”,如果没有找到,则移至下一个字段。到目前为止,这是我想出的,但没有用。有人可以帮忙吗?

var name = nameGlobal.value.toString();
 var regExp = new RegExp(name, "gi");
 var fields = [
  xfa.resolveNode("form1.#subform[3].#subform[4].N_Location.F1.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].N_Witnesses.Notes_2.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].F2.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_4.TextField7").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_5.TextField7").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_6.TextField9").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_7.N_7_2.TextField10").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_7.N_7_4.TextField11").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_7.N_7_6.TextField12").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_7.N_7_8.TextField13").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_7.F4.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F5.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F6.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F20.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.PupilSize.F7.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F8.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F9.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F10.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F11.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.#subform[20].TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_9.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Notes_10.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].F13.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].F14.TextField5").rawValue.toString(),
  xfa.resolveNode("form1.#subform[3].#subform[4].Msic_Notes.TextField5").rawValue.toString()
];

 for (i = 0; i < fields.length; i++) {
    if (fields[i] !== null) {
    fields[i] = fields[i].replace(name, "********");
     }
    }

替换不起作用的主要原因是 toString() 方法 returns 一个新字符串,而不是指向值的指针。

您可以通过放置节点的引用而不是 rawValue 字符串来解决问题。

使用一些旧的 javascript...

var name = nameGlobal.value; // might need to add toString() back
var regExp = new RegExp(name, "gi");
var fields = [xfa.resolveNode("path_to_node")];
 
for (var i = 0; i < fields.length; i++) {
  var rawValue == fields[i];
  if (rawValue !== null) {
    fields[i].rawValue = rawValue.replace(regExp, "********");
  }
}

使用现代 javascript...(不确定是否支持)

let name = nameGlobal.value; // might need to add toString() back
let regExp = new RegExp(name, "gi");
let fields = [ xfa.resolveNode("path_to_node")];
 
for (let field of fields) {
  let rawValue = field.rawValue
  if (!rawValue) { // might need to add the !== null
    field.rawValue = rawValue.replace(regExp, "********");
  }
}

希望对您有所帮助:)

所以我让它工作了,这就是我所做的:

var replace = nameGlobal.value.toString();
var regExp = new RegExp(replace, "gi");
var fields = [
  "form1.#subform[3].#subform[4].N_Location.F1.TextField5",
  "form1.#subform[3].#subform[4].N_Witnesses.Notes_2.TextField5",
  "form1.#subform[3].#subform[4].F2.TextField5",
  "form1.#subform[3].#subform[4].Notes_4.TextField7",
  "form1.#subform[3].#subform[4].Notes_5.TextField7",
  "form1.#subform[3].#subform[4].Notes_6.TextField9",
  "form1.#subform[3].#subform[4].Notes_7.N_7_2.TextField10",
  "form1.#subform[3].#subform[4].Notes_7.N_7_4.TextField11",
  "form1.#subform[3].#subform[4].Notes_7.N_7_6.TextField12",
  "form1.#subform[3].#subform[4].Notes_7.N_7_8.TextField13",
  "form1.#subform[3].#subform[4].Notes_7.F4.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F5.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F6.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F20.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.PupilSize.F7.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F8.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F9.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F10.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.F11.TextField5",
  "form1.#subform[3].#subform[4].Notes_8.ClinicalNOtes.#subform[20].TextField5",
  "form1.#subform[3].#subform[4].Notes_9.TextField5",
  "form1.#subform[3].#subform[4].Notes_10.TextField5",
  "form1.#subform[3].#subform[4].F13.TextField5",
  "form1.#subform[3].#subform[4].F14.TextField5",
  "form1.#subform[3].#subform[4].Msic_Notes.TextField5",
  ];

for (i = 0; i < fields.length; i++) {
    var val = xfa.resolveNode(fields[i]).rawValue;
    if (val !== null) {
        if (val.search(regExp) !== -1) {
            xfa.resolveNode(fields[i]).rawValue = val.replace(regExp, "*******").toString();
      }
    }
  }