Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all instances of character in string in typescript?

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!

like image 413
Rebecca Avatar asked Apr 09 '17 19:04

Rebecca


People also ask

How do you replace every instance of a character in a string?

To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag.

How do you replace all occurrences of a string?

replaceAll = String. prototype. replaceAll || function(string, replaced) { return this. replace(new RegExp(string, 'g'), replaced); };

How do you replace a section of a string in TypeScript?

The replace() method can be used in a TypeScript string to replace a particular substring or a match (regular expression) in a string. All we need to do is pass the substring or regular expression of the substring we want to replace.

How do you remove all occurrences of a character from a string in JavaScript?

Delete all occurrences of a character in javascript string using replaceAll() The replaceAll() method in javascript replaces all the occurrences of a particular character or string in the calling string. The first argument: is the character or the string to be searched within the calling string and replaced.


2 Answers

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)
like image 111
gyre Avatar answered Oct 08 '22 18:10

gyre


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.split('.').join('x'); 

So, it will replace all your . with x. So, after the above example, email variable become myxemail@gmailxcom

like image 39
Ajay Gupta Avatar answered Oct 08 '22 18:10

Ajay Gupta