Have some problems getting .replace to work when removing special characters from a string of special characters. For most special characters, just using the special character works. For others like $ or ^, using the special character doesn’t work.
Here’s what I’ve coded so far…
esc is a string of characters to remove.
var esc = “$&^*”;
msymbols is a string of all special characters. I want to remove the characters in esc from msymbols.
var msymbols = ‘~!@#$%^&*()_±={}|[]:";’ + “’” + ‘<>?,./’
I have a function to remove the characters.
function replaceGlobally(original, searchTxt, replaceTxt) {
const regex = new RegExp(searchTxt, ‘g’);
return original.replace(regex, replaceTxt) ;
};
I have a For loop that loops through esc one digit at a time. Based on the special character, the loop either sets the searchTxt value equal to the character at the digit location. Or it sets it to a different regex value to handle the special character. And then it calls the function to perform the substitution.
For $, the regex code \$ (note: there is two \s in my code - displaying here causes only one to show up) seems to work. The $ gets deleted from the msymbols string. But i can’t figure out what the appropriate code for the ^ is. And I know there are other special characters that have the same issue. Is there a cheat sheet somewhere I can use? Or is there a common pattern that will work for all of the one-one codes?
Okay - but the “esc” string will be different every time the program is run. So I won’t be able to hard code the searchTxt str for specific characters. That’s why I was using if statements to identify the substitution dependent on the character being replaced in that particular loop instance.
For example, I may have “$^%” one time. Another time I may have “!@#$”. And another time I may have “{})(*”. And the number of characters being replaced could go from 0 to 5 or 10 realistically. And knowing that some special characters don’t need to be replaced, I was looking for a way to deal with the ones that do.
Let me think. Dynamic. I can think of one long winded solution. Give me a second. Or honestly, if the prior code you had worked and you only need to tidy up what’s left
right now replaceTxt is used to replace a single character every time i loop through the string of characters being removed from the full string.
Another possible way to do this, is to loop thru the short string, and any characters that are in the long string and not in the short string are added to a new string. Not sure if I would have similar problems or not.
That appears to work. And very simple coding. Gets rid of all the if…then…else statements. Thanks for figuring this out and spending the time last night working on it. Appreciate it!
I believe so. You can try doing a condition with an “extract with regex” or “find and replace”. I haven’t tested it recently but I’ve done it. Someone else may be able to chime in and provide the regex string you need. If not, let me know what characters you are trying to extract.
@josh.jewison… I don’t know if this is what you want, but to keep, say, only letters, numbers, and spaces from a string (which means all special characters would be removed), this regex pattern would do the trick (note the space after the 9).