Replace all chars with #, except for last 4
To replace a character in a string at a given index, hash[i] = dd[i]
doesn't work. Strings are immutable in Javascript. See How do I replace a character at a particular index in JavaScript? for some advice on that.
How to replace the characters leaving last 4 letters
The following will work:
var input = "123456789";var output = input.replace(/.(?=.{4})/g, '*');console.log(output);
Replacing last 4 characters with a "*"
A quick and easy method...
public static String replaceLastFour(String s) {
int length = s.length();
//Check whether or not the string contains at least four characters; if not, this method is useless
if (length < 4) return "Error: The provided string is not greater than four characters long.";
return s.substring(0, length - 4) + "****";
}
Now all you have to do is call replaceLastFour(String s)
with a string as the argument, like so:
public class Test {
public static void main(String[] args) {
replaceLastFour("hi");
//"Error: The provided string is not greater than four characters long."
replaceLastFour("Welcome to StackOverflow!");
//"Welcome to StackOverf****"
}
public static String replaceLastFour(String s) {
int length = s.length();
if (length < 4) return "Error: The provided string is not greater than four characters long.";
return s.substring(0, length - 4) + "****";
}
}
How to replace all characters but for the first and last two with gsub Ruby
**If you want to mask with a fixed number of *
symbols, you may yse
'lorem.ipsum@gmail.com'.sub(/\A(..).*@.*(..)\z/, '\1****@****\2')
# => lo****@****om
See the Ruby demo.
Here,
\A
- start of string anchor(..)
- Group 1: first 2 chars.*@.*
- any 0+ chars other than line break chars as many as possible up to the last@
followed with another set of 0+ chars other than line break ones(..)
- Group 2: last 2 chars\z
- end of string.
The \1
in the replacment string refers to the value kept in Group 1, and \2
references the value in Group 2.
If you want to mask existing chars while keeping their number, you might consider an approach to capture the parts of the string you need to keep or process, and manipulate the captures inside a sub
block:
'lorem.ipsum@gmail.com'.sub(/\A(..)(.*)@(.*)(..)\z/) {
$1 + "*"*$2.length + "@" + "*"*$3.length + $4
}
# => lo*********@*******om
See the Ruby demo
Details
\A
- start of string(..)
- Group 1 capturing any 2 chars(.*)
- Group 2 capturing any 0+ chars as many as possible up to the last....@
-@
char(.*)
- Group 3 capturing any 0+ chars as many as possible up to the(..)
- Group 4: last two chars\z
- end of string.
Note that inside the block, $1
contains Group 1 value, $2
holds Group 2 value, and so on.
PHP remove all characters before, except last number
preg_match('/(\d+)\.ts$/', $test, $matches);
echo $matches[1];
How to substitute all characters in a string except for some (in Ruby)
Here's a different approach. First, do the reverse of what you ultimately want: redact what you want to keep. Then compare this redacted string to your original character by character, and if the characters are the same, redact, and if they are not, keep the original.
class String
# Returns a string with all words except those passed in as keepers
# redacted.
#
# "Part of this string needs to be substituted".gsub_except(["this string", "substituted"], '*')
# # => "**** ** this string ***** ** ** substituted"
def gsub_except keep, mark
reverse_keep = self.dup
keep.each_with_object(Hash.new(0)) { |e, a| a[e] = mark * e.length }
.each { |word, redacted| reverse_keep.gsub! word, redacted }
reverse_keep.chars.zip(self.chars).map do |redacted, original|
redacted == original && original != ' ' ? mark : original
end.join
end
end
Replace all the _ except one before last one
Use this (corrected now!)
<?php
$subject = 'http://distilleryimage3_s3_amazonaws_com/8af11cdcf11e286b022000ae90285_7_jpg';
$pattern = '/(_)(?!\d_jpg)/';
var_dump(preg_replace($pattern, '.', $subject));
This outputs
http://distilleryimage3.s3.amazonaws.com/8af11cdcf11e286b022000ae90285_7.jpg
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
Related Topics
Reload Table After Deleting an Item from It With React
How to Show an Alert After Reloading the Page in JavaScript
Executing Nodejs Script File in PHP Using Exec()
Detect If String Contains Any Spaces
How to Use Owl Carousel Vertically
Possible to Detect If a User Has Multiple Tabs of Your Site Open
Vuejs Set Active Class, When One Li Element Clicked in V-For Loop
How to Attach Multiple Images in a Embed
How to Mock the JavaScript Window Object Using Jest
Uploading Multiple Files Using Formdata()
How to Clear Location.State in React-Router on Page Reload
How to Reload Datatable by Ajax (Jquery)
Nodejs MySQL Get Correct Timestamp Format
React Enable Button After All Form Fields Are Not Empty
Slick Carousel - Force Slides to Have the Same Height
Onchange Event Not Fire When the Change Come from Another Function