简体   繁体   中英

How to replace last char of each words with X using jquery?

var str_owner = $("#owner").val();
var owner_length = str_owner.length/2;
var owner = str_owner.slice(0, -5)+"XXXXX";
$("#owner_name").html(owner);

In the above code I want to replace exact half of each word with X but here I am replace last 5 char with X and I am getting string like Mike hXXXXX but I want like MiXX husXXX . So, How can I do this? Please help me.

Thank You

Split the string first and modify the words you want:

let str_owner_words = $('#owner').val().split(' ');
for(let i = 0; i < str_owner_words.length; i++){

  const cut_length = Math.round(str_owner_words[i].length / 2);
  str_owner_words[i] = str_owner_words[i].slice(0, -1 * cut_length) + 
    "X".repeat(cut_length);

}

let result_string = str_owner_words.join(" ");

Try this.

Does this meets your requirement?

Logic

  • Select the contet from dom that needs to be encoded.
  • If the value consisit of more that one word, split the individual word at the mid, take the first half of the selected word and append 'X' to the remaining half.
  • If the word length is odd (say length is 7 as mentioned in example), fill the first 4 with the actual letters and next 3 with 'X'. If you need it in reverse pattern, ie, first 3 as actual letters and rest 4 as 'X', use Math.floor in the function.

 $(document).ready(function() { $('#replace').click(function(e) { var str_owner = $("#owner").text(); var split_str = str_owner.split(' '); var owner = ''; split_str.forEach((str) => { var owner_length = Math.ceil(str.length/2); var owner_visible_str = str.slice(0, owner_length); owner += owner_visible_str + 'X'.repeat(str.length - owner_visible_str.length) + ' '; }) $("#owner_name").html(owner); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <p id="owner">John Mark Stephen</p> <p id="owner_name"></p> <button id="replace">Replace</button>

As strings are arrays in JS, you can make two substrings and iterate over second on, and add x for each element of substring 2, in substring 1.

 var str = 'monty'; var sub1 = str.substr(0,Math.floor(str.length/2)); var sub2 = str.substr(Math.floor(str.length/2),str.length); for(let i of sub2){ sub1 +="x"; } console.log(sub1);

This is my solution for now. On purpose I added string of 4,2,1,5 character words.

 var str = 'This is a tests' var strArr = str.split(' ', ) var final_string_arr= []; let i = 0; while (strArr.length > i) { var this_str_length = parseInt(strArr[i].length/2); var remain = strArr[i].length - this_str_length; var remStr =""; if(this_str_length > 0){ remStr = strArr[i].slice(0, -(this_str_length))+"x".repeat(this_str_length); }else{ remStr = strArr[i]; } final_string_arr.push(remStr); i++; } console.log(final_string_arr.join(" "));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM