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
5 Answers
Reset to default 3Possible 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
版权声明:本文标题:javascript - find and replace everything before character jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745296043a2652082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论