how to remove special char-set from string except the first char

Viewed 218

I want to remove special characters, exist within the string, but except the first one. I did something like bellow and it works if they are not next to each other.

special char set = '❶❷❸❹❺❻❼❽❾❿➀'

My current code is:

let str = '❶Hi dear how❽ are❺ ❽you❼';
const len = str.length;
for(let i = 0; i < len; i++){
  if(i !== 0){
    if(str[i] === '❶' || str[i] === '❷' || str[i] === '❸' ||
    str[i] === '❹' || str[i] === '❺' || str[i] === '❻' || str[i] === '❼' ||
    str[i] === '❽' || str[i] === '❾' || str[i] === '❿' || str[i] === '➀'){
      str = str.replace(str[i], '');
    }
  }
}

console.log('output: ', str);

The above code works well, but if I change str like below then will not work:

let str = '❶Hi dear how❽ are❺❼ ❽❽you❼';

Expected output:

❶Hi dear how are you

Would be better if could be solve this with regex if it can be faster than my solutions

4 Answers

https://jsben.ch/Hcjqp

Ignore first character, do a straight replace. Benchmark above.

const replace = str => str[0] + str.slice(1).replace(/[❶-➀]+/g, '');
let str = '❶Hi dear how❽ are❺ ❽you❼'.repeat(2000);
console.log(replace(str));

The bug is subtle: when you remove a character from your string, you should also decrease the index, like this:

str = str.replace(str[i--], '');

... as you drop that character but leave the cursor at the same place, and move it forward at the very next step. That's why your original code failed to remove the repeated 'blacklisted' characters.

And yes, that's easy to do with regex replace:

const blacklistCharacterClass = /[❶❷❸❹❺❻❼❽❾❿➀]/g;
const rawString = '❶Hi dear how❽ are❺❼ ❽❽you❼';
const refinedString = rawString.replace(blacklistCharacterClass, (c, i) => i ? '' : c);
console.log(refinedString); // ❶Hi dear how are you

Below Regex will not work in firefox and safari...

regex: ([❶❷❸❹❺❻❼❽❾❿➀])(?<!^[❶❷❸❹❺❻❼❽❾❿➀])

It will replace all special character excpet first...

Code:

str = "❶Hi dear how❽ are❺❼ ❽❽you❼'"

console.log(str.replace(/([❶❷❸❹❺❻❼❽❾❿➀])(?<!^[❶❷❸❹❺❻❼❽❾❿➀])/gm,''))

I have assumed that the first of the special characters is not necessarily at the beginning of the line.

You can convert matches of the following regular expression to empty strings.

(?<=[❶❷❸❹❺❻❼❽❾❿➀].*)[❶❷❸❹❺❻❼❽❾❿➀]

Demo

The regex reads, "match one of the characters of interest that is preceded by a character of interest" (since those are the ones to be replaced with an empty string.

Note that the positive lookbehind cannot be anchored to the beginning of the line.


Since writing the above I've learned that I've misinterpreted the question (see comments below). I will leave it, however.

One way to do the replacements when it is known that the first character of the string is one of the special characters is to replace each match of the regular expression

(?<=.)[❶❷❸❹❺❻❼❽❾❿➀]

with an empty string; that is, replace each special character that is preceded by another character (and therefore is not the first character in the string).

Related