How to Replace Some Characters with Asterisks

How replace characters with asterisks (*) except first two characters

You could do it like this

const s = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";

function hideWord(w) {
if (w.length < 3) return w;
return w.substring(0, 2) + '*'.repeat(w.length-2);
}

console.log(s.split(" ").map(hideWord).join(" "));

Output is

Lo*** Ip*** is si**** du*** te** of th* pr****** an* ty********* in*******

Split the string into words and then map each word top its hidden version and rejoin. To map it simply return any 1 or 2 letter length words as they were and for others take a substring of the first two chars and fill the rest with asterisks.

You could also wrap the whole lot in a function if you wanted to be able to call it from multiple places and avoid redefining it.

function hideWords(s) {
return s.split(" ").map(hideWord).join(" ");
}

console.log(hideWords(s));

How To Replace Some Characters With Asterisks

You'll need a specific function for each. For mails:

function hide_mail($email) {
$mail_segments = explode("@", $email);
$mail_segments[0] = str_repeat("*", strlen($mail_segments[0]));

return implode("@", $mail_segments);
}

echo hide_mail("example@gmail.com");

For phone numbers

function hide_phone($phone) {
return substr($phone, 0, -4) . "****";
}

echo hide_phone("1234567890");

And see? Not a single regular expression used. These functions don't check for validity though. You'll need to determine what kind of string is what, and call the appropriate function.

How to replace some characters with asterisks except first four characters

Using substr() you do this.

PHP

<?php        
function hide_mail($email) {
$new_mail = "";
$mail_part1 = explode("@", $email);
$mail_part2 = substr($mail_part1[0],4); // Sub string after fourth character.
$new_mail = substr($mail_part1[0],0,4); // Add first four character part.
$new_mail .= str_repeat("*", strlen($mail_part2))."@"; // Replace *. And add @
$new_mail .= $mail_part1[1]; // Add last part.
return $new_mail;
}

echo hide_mail("example@gmail.com");
?>

Output

exam***@gmail.com

Replace characters with asterisk

I only found an iterative solution:

let email = "asdfg.hjkl@gmail.com"
let atSign = email.index(of: "@") ?? email.endIndex
let userID = email[..<atSign]
print(userID + email.suffix(from: atSign))

var lastLetterInx = email.index(before:atSign)

var inx = email.startIndex

var result = ""
while(true) {
if (inx >= lastLetterInx) {
result.append(String(email[lastLetterInx...]))
break;
}

if (inx > email.startIndex && email[inx] != ".") {
result.append("*")
} else {
result.append(email[inx])
}

inx = email.index(after:inx)
}

print (result)

How to replace all matching integer characters with asterisks

You need to take the substring of everything after the first character before you call replaceAll. For example,

Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int inputLine = in.nextInt();
String v = Integer.toString(inputLine), f = v.substring(0, 1);
System.out.println(f + v.substring(1).replaceAll(f, "*"));
}

Replace all characters of string to asterisks except first and last characters in PHP

function get_starred($str) {
$len = strlen($str);

return substr($str, 0, 1).str_repeat('*', $len - 2).substr($str, $len - 1, 1);
}

$myStr = 'YourName';
echo get_starred($myStr); //should show Y******e

Replace letters with asterisks except first and last character using MYSQL

There is a way to do it in mysql. Other answers may give PHP solution, but in mysql:

SELECT CONCAT(LEFT(text,1), REPEAT('*',LENGTH(text)-2), RIGHT(text,1)) AS text FROM exercise

simply concatenate the first letter, then your asterisks, then last letter.

Adding the PHP code just in case:

// New Connection
$db = new mysqli('localhost','user','pass','database');

$result = $db->query("SELECT CONCAT(LEFT(text,1), REPEAT('*',LENGTH(text)-2), RIGHT(text,1)) AS text FROM exercise");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
echo $row;
}
// Free result set
$result->close();
}

alternatively, doing the replacement in PHP 7.1 or later

// New Connection
$db = new mysqli('localhost','user','pass','database');

$result = $db->query("SELECT text FROM exercise");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
if (strlen($row) > 2) {
echo $row[0] . str_repeat('*', strlen($row)-2) . $row[-1]
}
else {
echo $row
}

}
// Free result set
$result->close();
}

Replace all digit and asterisk characters except at start of string using JavaScript

Try /(\s+(\S+\d+\S+)|\*\S+)/g

Demo

function replaceRegex( input ){  return input.replace( /(\s+(\S+\d+\S+)|\*\S+)/g, "" );}
console.log( replaceRegex( "MSFT *<E07004QY6W>" ) );console.log( replaceRegex( "WOOLWORTHS W1157" ) ); //output is WOOLWORTHS W1157 but it matches your rules console.log( replaceRegex( "GOOGLE*ADWS7924436927" ) );console.log( replaceRegex( "COLES 0829" ) );console.log( replaceRegex( "7-ELEVEN 2179" ) );console.log( replaceRegex( "COLES EXPRESS 1896" ) );


Related Topics



Leave a reply



Submit