Replace all instances of character in string in typescript?
Asked Answered
F

3

64

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!

Flowerpot answered 9/4, 2017 at 19:28 Comment(1)
new is a reserved word in javascriptEnkindle
M
98

Your 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)
Manganin answered 9/4, 2017 at 19:44 Comment(7)
I was going wrong with the /./gi, /\./ solved the issue! Thanks a million!Flowerpot
how to replace a character in a string based on index?Turning
@Turning What do you mean? Can you give me a desired example/output?Manganin
let theString = "george" , i want to replace the third character for example theString[2]="a" so now it would be 'gearge'Turning
in that case I would suggest: let string = 'george'; let result = string.slice(0, 2) + 'a' + string.slice(3);Manganin
thank you sounds good currently i am transforming it to array then do the change then transforming it to string i'll try your way asapTurning
Do not forget that g to make the expression global. I always do.Neurologist
F
78

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

Farther answered 14/6, 2019 at 10:24 Comment(4)
I think this should be the default approach for a basic search/replace over using any regex.Curd
This is brilliant!!Preamplifier
Thanks. It works only if we assign the output string of join method to the variable again like email = email.split('.').join('x');Offer
In case if you want just to remove a symbol globally, it's worth to not forget to add '', like this: email.split('.').join('')Scarbrough
P
14

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:

enter image description here

More info here: Property 'replaceAll' does not exist on type 'string'

Perspicuous answered 23/2, 2022 at 17:57 Comment(7)
But I already have "es2021" in "lib"Enfeoff
Then you shouldn't get Property 'replaceAll' does not exist on type 'string' error and may use replaceAll() String function. Or you mean that you get this error despite you have "es2021" in "lib"?Perspicuous
Yes, I get it despite having es2021 in libEnfeoff
Ensure that you are looking into correct tsconfig.json. Also that you have only one tsconfig.json in the project. Try to re-create the file again. And check this link: #63616986Perspicuous
When I put "es2021", it stopped recognizing the type HTMLElement. Wtf...Eugene
Oh, inserted DOM along with ES2021 and it worked alright.Eugene
In VS Code I did "Command Palette -> Go to project configuration (tsconfig)" and it said the project doesn't have a configuration, so that was the problem, it wasn't detecting my config. The error "Property 'replaceAll' does not exist on type 'string'" was from some sort of default VS Code config and adding ES2021 to "lib" in my config file had no effect cause it wasn't being used.Enfeoff

© 2022 - 2024 — McMap. All rights reserved.