我正在处理翻译项目,我需要在最后修改一些字符,例如,如果用户输入的字样如下:
键入字词:" yilanin" =>目标词(我想要的):" yilaNG"
" suyunin" =>的" suyuNG"
" kalinin" =>的" kaliNG"
" batinin" =>的"软化"
等等......但我有一个问题:我不想修改&#34; nin&#34; 字符,如果它们位于单词的中间或开头,如:< / p>
&#34; kinindan *&#34;&#34; =&GT; **&#34; kinindan&#34;
&#34; sininteki &#34;&#34; =&GT;的&#34; sininteki&#34;
&#34; nin&#34; =&gt;的&#34; nin的&#34;
&#34; ninkisi&#34; =&gt;的&#34; ninkisi&#34;
等等......我的意思是每个&#34; xxxnin&#34; 到&#34; xxxNG&#34; ,
&#34; xxxninxxx&#34; 至&#34; xxxninxxx&#34; (无修改),
&#34; nin&#34; 指向&#34; nin&#34; (无需修改)......
我试图非常清楚地解释我的问题,并且我希望你能理解......
$(document).ready(function(){
$("#ta_1").keyup(function(event) {
var text2 = $(this).val();
text2 = text2.replace(/([abcçdefgğhıijklmnoöprsştuüvyzABCÇDEFGHIİJKLMNOÖPRSTUÜVYZ])nin$/g, '$1NG');
$("#ta_1").val(text2);
});
});
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<textarea id="ta_1" rows="5" cols="28"></textarea>
</body>
</html>
&#13;
答案 0 :(得分:3)
您需要跟踪两个事件:keyup
(跟踪文本输入延续)和blur
(检查字符串输入结束)。然后,使用2种不同的替代品:
$(document).ready(function(){
$("#ta_1").on("keyup blur", function(event) {
var text2 = $(this).val();
if (event.type !== "blur") { // Trigger only on keyup
text2 = text2.replace(/([a-zA-ZçğıöşüÇİÖÜ])nin\b(?![a-zA-ZçğıöşüÇİÖÜ])([\s\S])/g, '$1NG$2');
}
else { // we have blur
text2 = text2.replace(/([a-zA-ZçğıöşüÇİÖÜ])nin$/, '$1NG');
}
$("#ta_1").val(text2);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="ta_1" rows="5" cols="28"></textarea>
keyup
事件将检查已输入的字,其中包含(我猜)土耳其语单词([a-zA-ZçğıöşüÇİÖÜ])nin\b(?![a-zA-ZçğıöşüÇİÖÜ])([\s\S])
- 后跟一些将用作单词边界的字符。
blur
事件将仅在整个字符串的末尾检查nin
,并在必要时执行替换。
<强>更新强>:
这是另一种方法:在键入时替换,如果键入了另一个字符,则还原。但是,如果可以用一个相同的替换模式替换多个字符组合,那么这种方法将不起作用(将不清楚要恢复的字符串):
$(document).ready(function(){
$("#ta_1").on("keyup", function(event) {
var text2 = $(this).val();
// Replace nin to NG in general
text2 = text2.replace(/([a-zA-ZçğıöşüÇİÖÜ])nin\b(?![a-zA-ZçğıöşüÇİÖÜ])/g, '$1NG');
// Restore nin from NG
text2 = text2.replace(/([a-zA-ZçğıöşüÇİÖÜ])NG(?=[a-zA-ZçğıöşüÇİÖÜ])/g, '$1nin')
$("#ta_1").val(text2);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="ta_1" rows="5" cols="28"></textarea>
答案 1 :(得分:2)
这样的事情会起作用吗?
text.replace(/([\s\S])nin\b(?![çğıöşüÇİÖÜ])/, '$1NG');
\b
匹配单词边界位置,例如空格,标点符号或字符串的开头/结尾。
?![çğıöşüÇİÖÜ]
否定前瞻,如果找到任何指定的字符,则指定不匹配。
var stringArr = ['suyunin', 'kalinin', 'batinin', 'kinindan', 'sininteki', 'nin', 'ninkisi', 'xxxnin', 'xxxninxxx', 'nin', 'Kalininü'];
var generated = document.getElementById('generated');
for (var i = 0; i < stringArr.length; i++) {
var newStr = stringArr[i].replace(/([\s\S])nin\b(?![çğıöşüÇİÖÜ])/, '$1NG');
var li = document.createElement('li');
li.appendChild(document.createTextNode(newStr));
generated.appendChild(li);
}
#generated {
color: red;
}
<!DOCTYPE html>
<html>
<body>
Array List:
<br>['suyunin', 'kalinin', 'batinin', 'kinindan', 'sininteki', 'nin', 'ninkisi', 'xxxnin', 'xxxninxxx', 'nin'];
<br>Generated List:
<br>
<span id='generated'></span>
</body>
</html>
答案 2 :(得分:2)
这应该有效
(?!^nin)(nin$)
JS代码
var re = /(?!^nin)(nin$)/gm;
var str = 'suyunin\nabcninshs\nning\ngnin\nning\nkinindan*\nsininteki\nnin\nninkisi';
var subst = 'NG';
var result = str.replace(re, subst);
document.writeln(result)
&#13;
<强> JSFiddle Demo 强>
<强> Ideone Demo 强>
答案 3 :(得分:2)
试试这个
(\w)(nin)\b
$(document).ready(function(){
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 1000; //time in ms, 1 second for example
var $input = $('#myInput');
//user is "finished typing," do something
function doneTyping () {
//do something
}
$("#ta_1").keyup(function(event) {
var text2 = $(this).val();
var re = /(\w)(nin)([^a-zA-Z])/g;
var subst = '$1NG$3';
text2 = text2.replace(re, subst);
var re = /(\w)(NG)(\w)/;
var subst = '$1nin$3';
text2 = text2.replace(re, subst);
$("#ta_1").val(text2);
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
$("#ta_1").keydown(function(event) {
clearTimeout(typingTimer);
});
function doneTyping () {
var text2 = $("#ta_1").val();
var re = /(\w)(nin)\b/g;
var subst = '$1NG';
text2 = text2.replace(re, subst);
$("#ta_1").val(text2);
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<textarea id="ta_1" rows="5" cols="28"></textarea>
</body>
</html>