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 answers
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 β¦
|
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 : "*") |
string.replace(/(?<=^(?:.{2})).*?(?=(?:.{2})$)/, '')- Let's say Pie