Get rid of special characters in string

Not a bubble question - a javastring question.

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(i=0; i < esc.length; i++){
if(esc[i] == “$”) {
searchTxt = “\$”;
} else if(esc[i] == “^”) {
searchTxt = “\^”;
} else {
searchTxt = esc[i];
}
replaceTxt = “”;
msymbols = replaceGlobally(msymbols, searchTxt, replaceTxt);
};

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?

clean your string the way you are, since it’s working and then add something like

let cleanedString = replaceTxt.replace(’$’, ‘’);

Or better yet:

var esc = "$&^*";
var msymbols = '~!@#$%^&*()_±={}|[]:";’' + "'" + '<>?,./';

function replaceGlobally(original, searchTxt, replaceTxt) {
  const regex = new RegExp(searchTxt.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
  return original.replace(regex, replaceTxt);
}

for (var i = 0; i < esc.length; i++) {
  var searchTxt = esc[i].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  var replaceTxt = '';
  msymbols = replaceGlobally(msymbols, searchTxt, replaceTxt);
}

console.log(msymbols);

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

maybe this will work?

Isn’t that just replacing the single character in the replaceTxt field with a blank. And not replacing the character in the long string of characters?

To replace all you would let cleanedString = replaceTxt.replace(/$/, ‘something’);

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.

You can do away with the iteration and just have

function removeEscChars(msymbols, esc) {
    var escRegExp = new RegExp('[' + esc + ']', 'g');
    return msymbols.replace(escRegExp, '');
}

This way we create a regexp for esc and then replace what we want.

Does that require the characters in esc to be in the same order in msymbols that they are in esc? They usually won’t be.

No I don’t believe so.

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!

1 Like

Sorry to sidebar this question but is there a way to do exactly what you’re talking about with just bubble and no coding?

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).

Even though it’s an easy pattern, here it is if you want to copy/paste it: ^[a-zA-Z0-9 ]*

This topic was automatically closed after 70 days. New replies are no longer allowed.