typescript 查找替换从当前位置往后查找

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 查找替换从当前位置往后查找相关的知识,希望对你有一定的参考价值。

// TeXworksScript
// Title: Search and Replace
// Description: search and replace like word (support wildcase)
// Author: KR
// Version: 0.1
// Date: 2019-1-17
// Script-Type: standalone
// Context: TeXDocument
// Shortcut: Ctrl+shift+H




function isEmptyObject(e) {  
    var t;  
    for (t in e)  
        return !1;
    return !0;
} 


var myFirstForm = null; // main form
var myThirdForm = null;
var myForms = [];

// any time later or straight a way

myFirstForm = TW.createUI("../Lib/searchRepDialog.ui");
myForms.push(myFirstForm);
//   TW.information(null,"s",my)
//eval("var " + pushButton + " = TW.findChildWidget(myDialogue,\"" + pushButton + "\")");
var pushButton = TW.findChildWidget(myFirstForm,"pushButton")
var replaceButton = TW.findChildWidget(myFirstForm,"pushButton_3")
var checkBox = TW.findChildWidget(myFirstForm,"checkBox")
var textEdit = TW.findChildWidget(myFirstForm,"plainTextEdit")
var textReplace = TW.findChildWidget(myFirstForm,"plainTextEdit_2")
var findText = "";
var findCount= 0
var  preIndex  = 0;
var oriStart = TW.target.selectionStart;  
function getSubMatchesIndex(index){

    if (checkBox.checked == true ) {
        findText = textEdit.plainText;
       
        // TW.information(null,"s",findText)
        findText = findText.replace("@","+?");        
        findText = findText.replace("{1,}","+");
        findText = findText.replace(".","\.");
        
    }else{
        findText = textEdit.plainText
        findText = findText.replace(/(\W)/g,"\\$1")
    }
    //TW.information(null,"s",textEdit.modificationChanged())
    var reg = new RegExp(findText,"g"); 

    TW.target.selectAll();
    var selAll = TW.target.selection;   
    TW.target.selectRange(oriStart,selAll.length-oriStart);
    //TW.information(null,"s","s")
    var dStart = TW.target.selectionStart;    
    var SELECTION = TW.target.selection; 
    var matches = SELECTION.match(reg)
    if (!isEmptyObject(matches)){
        if (index === matches.length){
            TW.information(null,"find and replace","We have finished searching the document")  
            
            findCount= -1

        }
        while (index> matches.length){
            index = index - matches.length 
        }
            //TW.information(null,"find and replace","Cannot finds")            
        
        for (var i=0; i<matches.length; i++){
            
            reg.test(SELECTION)
            
             if (i==index){
                
                
                TW.target.selectRange(oriStart + reg.lastIndex-matches[i].length, matches[i].length) 
                if (TW.target.selectionStart ==preIndex){
                    reg.test(SELECTION)
                    TW.target.selectRange(oriStart +reg.lastIndex-matches[i+1].length, matches[i+1].length) 
                }
                preIndex  = TW.target.selectionStart;
                //TW.information(null,"find and replace",preIndex);
                //TW.information(null,"find and replace",i)    
                                    
            }
            
        }
        
    }else{
        TW.information(null,"find and replace","Cannot find")
    }
    
}


var preText = textEdit.plainText;


function searchReplace(){
    if (textEdit.plainText.length< 1) {
        TW.information(null,"Search and Replace","please insert find text");
        
    }else{
        if (textEdit.plainText != preText && findCount!=0 ){
            findCount= 0;
        }
        getSubMatchesIndex(findCount);
        //TW.information(null,"s",index);

        //TW.target.selectRange(index-4,textEdit.plainText.length)
        
        //TW.target.selectRange(dStart+firstIndex ,myArray[1].length);

        findCount = findCount +1;
        //TW.information(null,"s",findCount);
        preText = textEdit.plainText;
    }
}

function replaceText(){

    if (checkBox.checked == true ) {    
        findText = textEdit.plainText;
        findText = findText.replace("@","+?");        
        findText = findText.replace("{1,}","+");  
        findText = findText.replace(".","\.");
        newRep = textReplace.plainText.replace("\\","$");
        newText = TW.target.selection.replace(new RegExp(findText,"g"),newRep);
        TW.target.insertText(newText);
    }else{
        TW.target.insertText(textReplace.plainText);
    }    
    findCount -=1
    searchReplace()
    
}

pushButton.clicked.connect(searchReplace)
replaceButton.clicked.connect(replaceText)

myFirstForm.exec();
TW.target.selectRange(oriStart);
myFirstForm.deleteLater();
//myFirstForm.show();
//myFirstForm.hide();

//myForms.setVisible(true); // there will be a momentary flicker

//   var  answer = myForm.exec();





以上是关于typescript 查找替换从当前位置往后查找的主要内容,如果未能解决你的问题,请参考以下文章

字符串替换空格

js字符串方法

从后台服务android查找当前位置

vi编辑器下如何查找字符串

从当前位置IOS查找自动完成结果位置的距离

二分查找