admin管理员组文章数量:1136389
I'm trying to replace all full stops in an email with an x character - for example "[email protected]" would become "myxemail@emailxcom". Email is set to a string.
My problem is it's not replacing just full stops, it's replacing every character, so I just get a string of x's.
I can get it working with just one full stop, so I'm assuming I'm wrong on the global instance part. Here's my code:
let re = ".";
let new = email.replace(/re/gi, "x");
I've also tried
re = /./gi;
new = email.replace(re, "x");
If anyone can shed any light I'd really appreciate it, I've been stuck on this for so long and can't seem to figure out where I'm going wrong.
** Edit: Whoops, my new variable was actually called newemail, keyword new wasn't causing the issue!
I'm trying to replace all full stops in an email with an x character - for example "[email protected]" would become "myxemail@emailxcom". Email is set to a string.
My problem is it's not replacing just full stops, it's replacing every character, so I just get a string of x's.
I can get it working with just one full stop, so I'm assuming I'm wrong on the global instance part. Here's my code:
let re = ".";
let new = email.replace(/re/gi, "x");
I've also tried
re = /./gi;
new = email.replace(re, "x");
If anyone can shed any light I'd really appreciate it, I've been stuck on this for so long and can't seem to figure out where I'm going wrong.
** Edit: Whoops, my new variable was actually called newemail, keyword new wasn't causing the issue!
Share Improve this question edited Apr 9, 2017 at 19:56 Rebecca asked Apr 9, 2017 at 19:28 RebeccaRebecca 6731 gold badge5 silver badges8 bronze badges 1 |3 Answers
Reset to default 104Your second example is the closest. The first problem is your variable name, new
, which happens to be one of JavaScript's reserved keywords (and is instead used to construct objects, like new RegExp
or new Set
). This means that your program will throw a Syntax Error.
Also, since the dot (.
) is a special character inside regex grammar, you should escape it as \.
. Otherwise you would end up with result == "xxxxxxxxxxxxxxxxxx"
, which is undesirable.
let email = "[email protected]"
let re = /\./gi;
let result = email.replace(re, "x");
console.log(result)
You can try split()
and join()
method that was work for me. (For normal string text)
It was short and simple to implement and understand.
Below is an example.
let email = "[email protected]";
email = email.split('.').join('x');
So, it will replace all your .
with x
. So, after the above example, email
variable will have value myxemail@gmailxcom
You may just use replaceAll()
String function, described here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
If you are getting
Property 'replaceAll' does not exist on type 'string'
error - go to tsconfig.json
and within "lib" change or add "es2021".
Like this:
More info here: Property 'replaceAll' does not exist on type 'string'
本文标签: javascriptReplace all instances of character in string in typescriptStack Overflow
版权声明:本文标题:javascript - Replace all instances of character in string in typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736952274a1957475.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
new
is a reserved word in javascript – charlietfl Commented Apr 9, 2017 at 19:33