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

时间:2015-04-09 08:49:35

标签: livecode

我如何替换begin {document}和end {document}之间的内容。在使用我的代码时,整个内容都被替换。在下面的代码中,我使用的是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

1 个答案:

答案 0 :(得分:0)

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

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

而只是'fld“MytextField”'会给出

foo
begin{document}

所以第一个的偏移(“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,如果你记住它给你字数,那么你必须替换{{1} }。