admin管理员组

文章数量:1418636

I have to display client information on site side for that I'm displaying email and other information as well.

Here I have to replace all the characters before @ with * like

[email protected]

This is the example mail id I want result as

****@gmail

I tried below one only @ is replacing with *

$('.element span').each(function() {
  console.log($(this).text());
  var text = $(this).text().replace('@', '*');
  $(this).text(text);
});
<script src=".1.1/jquery.min.js"></script>
<div class="element">
  <span>[email protected]</span>
</div>

I have to display client information on site side for that I'm displaying email and other information as well.

Here I have to replace all the characters before @ with * like

[email protected]

This is the example mail id I want result as

****@gmail.

I tried below one only @ is replacing with *

$('.element span').each(function() {
  console.log($(this).text());
  var text = $(this).text().replace('@', '*');
  $(this).text(text);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">
  <span>[email protected]</span>
</div>

How can I achieve this?

Thanks in advance.

Share Improve this question edited Apr 24, 2017 at 11:08 George 6,7492 gold badges30 silver badges36 bronze badges asked Apr 24, 2017 at 11:07 user3668438user3668438 1755 silver badges20 bronze badges 10
  • 3 Is this for security purposes? I assume you realise this is VERY low security? – mayersdesign Commented Apr 24, 2017 at 11:08
  • I have to agree with @mayersdesign you'd still be sending the full email address, all anyone would have to do is open their network tab. – George Commented Apr 24, 2017 at 11:09
  • ...or disable javascript and hit F5 – mayersdesign Commented Apr 24, 2017 at 11:10
  • @mayersdesign Is correct, the user can easily view the source of the page without the JS run, and if you'll use AJAX, they can see the results from the server via developer tools. You need to do it on the server side – Alon Eitan Commented Apr 24, 2017 at 11:11
  • 2 @AlonEitan for showing purpose he can do that(replace with ****) but if he is sending that mail id through ajax ,then instead of sending mail id send user-id and then fetch his mail id. this much only possible. – Death-is-the-real-truth Commented Apr 24, 2017 at 11:16
 |  Show 5 more ments

5 Answers 5

Reset to default 3

Possible solution.

$('.element span').each(function() {
  $(this).text($(this).text().replace(/.+(?=@)/g, '*'.repeat($(this).text().replace(/@.+/g, '').length)));
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">
   <span>[email protected]</span>
</div>

<div class="element">
   <span>[email protected]</span>
</div>

Another possible solution, without regex:

document.querySelectorAll('p').forEach(function(el){
    var text = el.innerText;
    var substr = text.substr(0, text.lastIndexOf('@'));
    el.innerText = text.replace(substr, '*'.repeat(substr.length));
});
<p>[email protected]</p>
<p>[email protected]</p>
<p>[email protected]</p>
<p>[email protected]</p>

Try to change your jQuery script to following script:

$(function(){
    $('.element span').each(function() {
        var index = $(this).text().indexOf("@");
        var substring = $(this).text().substr(0, index);
        var otherpart = $(this).text().substr(index);

        for (var i = 0, len = substring.length; i < len; i++) {
            substring = substring.replace(substring[i], '*');
        }
        console.log(substring + otherpart);
        $(this).text(substring + otherpart);
    });
});

Let me know if it is works.

try below code.

`<!DOCTYPE html>
 <head>
 </head>
 <body>
 <script 
 src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="element">
<span>[email protected]</span>
</div>
<script>

function replaceRange(s, start, end, substitute) {
return s.substring(0, start) + substitute + s.substring(end);
}

$('.element span').each(function() {
var str = $(this).text();
console.log(str);
console.log(str.length)
var i = 0;
var ind = 0;
var text = "";
var repchar = "";
while (i < str.length) {
console.log(str.charAt(i));
if(str.charAt(i) == '@')
{
ind = i;
break;
}
i++;
}
console.log(ind);
 for (i = 0; i < ind; i++)
 {
repchar  += "*";
 }

 text = replaceRange(str, 0, i , repchar); 
  console.log(text);
  $(this).text(text);
   });
   </script>
   </body>
   </html>`

This is also a solution

var strArray = ("[email protected]").split("@"); var str = strArray[0].replace(/./gi, '*'); str =str +"@"+ strArray[1];

本文标签: javascriptfind and replace everything before character jqueryStack Overflow