[英]Replace specific words in textarea and highlight changes?
I am working on a very large project and having to convert a lot FoxPro to C#. 我正在做一个非常大的项目,必须将很多FoxPro转换为C#。 I am putting together a tool to help me convert the bulk of the code (that I keep find/replacing anyways) and by no means do I use it as a fix all.
我正在整理一个工具来帮助我转换大部分代码(无论如何我都会不断查找/替换),并且决不将其用作修复程序。 What I'm trying to accomplish is where a common word in FoxPro in the input textarea ("Else" for example) will be replaced with it's C#/Javascript counterpart ("} else {") and the newly converted block of code will be shown in the output textarea.
我要完成的工作是将输入文本区域中FoxPro中的一个常用单词(例如“ Else”)替换为C#/ Javascript对应项(“} else {”),并将新转换的代码块替换为显示在输出文本区域中。 So I'm creating a list of replacements:
因此,我正在创建替换列表:
var replacements = [
["?thisform.", "@"],
["Else", "} else {"],
["*--", "//"]
];
and for each first word found in the input textarea, replace with the second word and paste the entire block in the output. 对于在输入文本区域中找到的每个第一个单词,请替换为第二个单词,并将整个块粘贴到输出中。 I have some moderately working examples in the fiddle.
我在提琴中有一些适度工作的例子。
$('#convert').click(function(){
var input = $('#codeinput').val();
replacements.forEach(function(pair) {
converted = input.split(pair[0]).join(pair[1]);
});
//var converted = $("#codeinput").val()
//.replace("?thisform.", "@");
// and paste final output
$('#codeoutput').val(converted);
});
BONUS POINTS Can we also highlight the changed words in the input and output (i understand we cannot highlight in textareas so maybe make this a div instead)? 奖励要点我们还可以在输入和输出中突出显示更改的单词吗(我知道我们不能在textareas中突出显示,所以可以将其改为div)? http://jsfiddle.net/aa72vg2c/10/
http://jsfiddle.net/aa72vg2c/10/
You overwriting "converted" with last input replace, so only last value is replaced. 您用最后一个输入替换覆盖“转换”,因此仅替换了最后一个值。 And why use split join instead simple replace?
为什么使用拆分联接而不是简单替换? It should look like this:
它看起来应该像这样:
converted = input;
replacements.forEach(function(pair) {
converted = converted.replace(pair[0], pair[1]);
});
If you wanna colors you can always do something like this: 如果您想着色,可以随时执行以下操作:
converted = converted.replace(pair[0], '<span class="someclass">'+pair[1]+'</span>');
and place it into div instead of text area. 并将其放入div而不是文本区域。
why not using replace as you first did? 为什么不像您第一次那样使用replace?
var replacements = [
["?thisform.", "@"],
["Else", "} else {"],
["*--", "//"],
["\n", "<br>"],
];
$('#convert').click(function(){
var input = $('#codeinput').val();
replacements.forEach(function(pair) {
input = input.replace( pair[0], '<span class="high">'+ pair[1]+'</span>', "g");
});
// and paste final output
$('#codeoutput').html(input);
});
add the g
flag to replace
to get all occurences. 添加
g
标志进行replace
以获取所有出现次数。
Add divs input
and output
, and this should do it: 添加divs
input
和output
,应该这样做:
$('#convert').click(function(){
var input = output = $('#codeinput').val();
for(var val in array) {
input= input.split(val).join('<span class="high">'+val+'</span>');
output= output.split(val).join('<span class="high">'+array[val]+'</span>');
}
$('#input').html('<pre>'+input+'</pre>');
$('#output').html('<pre>'+output+'</pre>')
});
http://jsfiddle.net/4su60eLm/1/ http://jsfiddle.net/4su60eLm/1/
Global replacement would be easy task with Regexp: 使用Regexp,全局替换将是一件容易的事:
var replacements = [
["Else", "} else {"],
["\\*\\-\\-", "//"],
["\\?thisform.", "@"]
];
$('#convert').click(function(){
var input = $('#codeinput').val();
$.each(replacements, function(index, value){
input = input.replace(new RegExp(value[0], 'g'),
'<span class="high">'+ value[1]+'</span>');
});
$("#codeoutput").html(input);
});
And pay much attention to escape special characters for Regexp - see first array values in my example. 并且要特别注意转义Regexp的特殊字符-请参见示例中的第一个数组值。
Working jsfiddle: http://jsfiddle.net/aa72vg2c/13/ 工作的jsfiddle: http : //jsfiddle.net/aa72vg2c/13/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.