我如何替换 begin{document} 和 end{document} 之间的内容

How i replace the content between begin{document} and end{document}

我如何替换 begin{document} 和 end{document} 之间的内容。在使用我的代码时,全部内容都在 replaced.In 下面的代码中,我正在使用 wordOffset,但它不起作用

on mouseUp
    put wordOffset("begin{document}",fld "MytextField") into tBegin 
    put wordOffset("end{document}",fld "MytextField") into tEnd
    put the htmlText of field "MytextField" into myHtml
    --put wordOffset("begin{document}",fld "myHtml") into tBegin
    --put wordOffset("begin{document}",fld "myHtml") into tEnd
     set the caseSensitive to true
     put the field SRText into myArrayToBe
     split myArrayToBe by CR
     --enable the field "SRText"
     --put "red,RED" & CR & "green,GREEN" & CR & "blue,BLUE" into myArrayToBe 
     --split myArrayToBe by CR
     put the number of lines of (the keys of myArrayToBe) into myArraylength
     repeat with i = 1 to myArraylength 
        --return i
        put  myArrayToBe[i] into y
        split y by colon
        put y[1] into searchStr
        put y[2] into replaceStr
        if searchStr is empty then
           put the  0 into m
        else 
           --put (word tBegin to tEnd of fld "MytextField")
           --put replaceText(word tBegin to tEnd of fld "MytextField","searchStr","good") into word tBegin to tEnd of fld "MytextField"
           put wordOffset("begin{document}",fld "MytextField") into tBegin 
           put wordOffset("end{document}",fld "MytextField") into tEnd
           --put holder into  myHtml
           --put replaceText(word tBegin to tEnd of fld "MytextField",searchStr,replaceStr)of fld "MytextField"
          replace searchStr  with  "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtml

      end if
    end repeat
    --enable me
    set the htmlText of fld "MytextField" to myHtml

end mouseUp

您需要使用 htmlText 来获取和设置文本,否则您的偏移量将关闭(例如,如果字段的第一行是粗体,htmlText 会给出类似于

<b>foo</b><br>begin{document}

而 'fld "MytextField"' 会给出

foo
begin{document}

so offset("begin{document}",...) 第一个给出 16,而第二个给出 5。我要做的是

put the htmlText of fld "MytextField" into theHTML
put offset("begin{document}", theHTML) into tBegin 
put offset("end{document}", theHTML) into tEnd
add length of "begin{document}" to tBegin
put character tBegin to tEnd of theHTML into textToChange

-- now do your search/replace on textToChange, not the field

put textToChange into character tBegin to tEnd of theHTML
set the htmlText of fld "MytextField" to theHTML

我在这里使用 offset() ,它给出了一个字符偏移量,但是 wordOffset 应该也可以工作,如果你记住它给你单词编号,所以你必须替换 word tBegin to tEnd of theHTML.