Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ace js replace text

Tags:

javascript

i got a problem with ace.js. I need to select a word an then insert the word without the occurance of =. This is my code:

editor.$search.setOptions({needle: '[A-z,0-9,_,\-]*="[A-z,0-9,_,\-,.]*"', regExp:true});
            ranges = editor.$search.findAll(editor.session);
            randomRange = ranges[Math.floor(Math.random() * ranges.length)];
            if (randomRange){
                editor.selection.setRange(randomRange);
                var content = editor.session.getTextRange(editor.getSelectionRange());
                alert(content);
                var result = content.replace('=', ''); 
                alert(result);
                editor.replace(result); 
            }

It selects a random occurance of that text. Then i replace the = with a space. But ace.js always replaces the text with the second occurance of the matched element, not with the first selected. Any suggestions? TY!

UPDATE: example:

<?xml version="1.0"?>
<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">

it selects version="1.0", deletes the =. then it´s version"1.0"

but now when i call the function editor.replace(result), it replaces PurchaseOrderNumber="99503" with that text so then its:

<?xml version="1.0"?>
<PurchaseOrder version"1.0" OrderDate="1999-10-20">
like image 260
m1crdy Avatar asked Jun 16 '14 08:06

m1crdy


1 Answers

GOT IT! this should be the replace statement:

editor.session.replace(editor.selection.getRange(), result);   
like image 106
m1crdy Avatar answered Oct 06 '22 11:10

m1crdy