How to replace all the characters in the line except the first two and the last two?

Closed due to the fact that the essence of the question is not clear to the participants of Wiktor StribiΕΌew , Deonis , Kromster , user300000, Andrew NOP 9 Oct '18 at 13:49 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    What to replace? Describe your problem. - Wiktor StribiΕΌew
  • replace with special character * - werty
  • string.replace(/(?<=^(?:.{2})).*?(?=(?:.{2})$)/, '') - Let's say Pie

3 answers 3

The question is asked with an extensive field for fantasies)) I'll try and I wallow:

 const str = 'Как Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ всС символы Π² строкС ΠΊΡ€ΠΎΠΌΠ΅ ΠΏΠ΅Ρ€Π²Ρ‹Ρ… Π΄Π²ΡƒΡ… ΠΈ послСдних Π΄Π²ΡƒΡ…'; // Π—Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ всС символы ΠΎΠ΄Π½ΠΈΠΌ символом * console.log(str.replace(/^(.{2}).*(.{2})$/g, '$1*$2')); // ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ символ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ символом * console.log(str.substr(0, 2) + str.slice(2,-2).replace(/./g,'*') + str.substr(-2)); // На всякий случай, ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ символ, ΠΊΡ€ΠΎΠΌΠ΅ ΠΏΡ€ΠΎΠ±Π΅Π»ΡŒΠ½Ρ‹Ρ…, Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ символом * console.log(str.substr(0, 2) + str.slice(2,-2).replace(/[^\s]/g,'*') + str.substr(-2)); 

     console.log("Как Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ всС символы Π² строкС ΠΊΡ€ΠΎΠΌΠ΅ ΠΏΠ΅Ρ€Π²Ρ‹Ρ… Π΄Π²ΡƒΡ… ΠΈ послСдних Π΄Π²ΡƒΡ… ?".split(" ").map( s => s.replace(/^(..).*(..)$/, "$1$2") ).join("\n")) 
     .as-console-wrapper.as-console-wrapper { max-height: 100vh } 

    • You have no exact error? :) - Let's say Pie
    • @ Let'ssayPie, where? - Qwertiy ♦
    • In my opinion, not everything is replaced here. how to i.stack.imgur.com/zwIS7.png - Let's say Pie
    • @ Let'ssayPie, I have no idea what you are running. Here: i.stack.imgur.com/gnmYx.png - Qwertiy ♦

    Regulars in your case are not needed. Such code may be somewhat more efficient:

     let str = "Как Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ всС символы Π² строкС ΠΊΡ€ΠΎΠΌΠ΅ ΠΏΠ΅Ρ€Π²Ρ‹Ρ… Π΄Π²ΡƒΡ… ΠΈ послСдних Π΄Π²ΡƒΡ…?"; let res = [...str].map((e, i) => i<2 || i>=str.length-2 ? e : "*").join(""); console.log(res); 

    And it will be easier to modify it, for example, if you do not need to change the spaces:

     .map((e, i) => i<2 || i>=str.length-2 || e==" " ? e : "*")