pinug
pinug

Reputation: 211

regex to modify characters in specific case at the end of a word

im working on a translation project and i need to modify some characters at the end for example if user types words like:

typed word: "yilanin" => target word (what i want) :"yilaNG"

"suyunin" => "suyuNG"

"kalinin" => "kaliNG"

"batinin" => "batiNG"

etc etc...

But i have a problem: i don't want to modify "nin" characters if they are at the middle or at the beginning of a word like:

"kinindan*"" => **"kinindan"

"sininteki"" => "sininteki"

"nin" => "nin"

"ninkisi" => "ninkisi"

etc etc...

i mean every "xxxnin" to "xxxNG",

"xxxninxxx" to "xxxninxxx"(no modification),

"nin" to "nin" (no modification)...

i tried to explain my problem very clearly and bascially i hope you understand...

$(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);
  
});
  
});
<!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>

Upvotes: 2

Views: 153

Answers (4)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You need to track two events: keyup (to track text input continuation) and blur (to check for the string input end). Then, use 2 different replacements:

$(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>

The keyup event will check for an already input word containing (I guess) Turkish word - ([a-zA-ZçğıöşüÇİÖÜ])nin\b(?![a-zA-ZçğıöşüÇİÖÜ])([\s\S]) - followed with some character that will serve as a word boundary.

The blur event will check for a nin at the end of the whole string only, and perform the replacement if necessary.

Update:

Here is another approach: replace while typing and if another character is typed, revert. However, this approach won't work if several character combinations can be replaced with one and the same replacement pattern (it will be unclear what string to restore then):

$(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>

Upvotes: 3

timolawl
timolawl

Reputation: 5564

Would something like this work?

text.replace(/([\s\S])nin\b(?![çğıöşüÇİÖÜ])/, '$1NG');

\b matches a word boundary position such as whitespace, punctuation, or the start/end of the string.

?![çğıöşüÇİÖÜ] negative lookahead to specify no match if any of the specified characters are found.

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>

Upvotes: 2

Tim007
Tim007

Reputation: 2557

Try this

(\w)(nin)\b

Regex demo

$(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>

Upvotes: 2

rock321987
rock321987

Reputation: 11032

This should work

(?!^nin)(nin$)

Regex Demo

JS Code

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)

JSFiddle Demo

Ideone Demo

Upvotes: 2

Related Questions