javascript - find and replace everything before character jquery - Stack Overflow

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

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];

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745296043a4621146.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信