Reputation: 1338
I'm looking forward for a simple solution how to erase these array of chars from a specific string
const ignoreMe = ['a', 'b', 'c'];
so if my string contains one of these chars it will return it without them example: "abcde" will return "de", * one of these chars may appear or not and may even appear multiple times so I need to handle all of these cases.
I was thinking about running on every char in my string and then to see if it's equal to one of them but I think it's too much, is there a simpler way to do it ?
thanks.
Upvotes: 0
Views: 44
Reputation: 6501
If you have an array of single charecters, you can do something like this:
let str = 'aaaxxxbbbyyyccc';
const ignoreMe = ['a', 'b', 'c'];
const cleanStr = str.replace(new RegExp(`[${ignoreMe.join()}]`, 'g'), '');
console.log(cleanStr);
Upvotes: 0
Reputation: 171679
Using array approach you can split()
the string into an array and use Array#filter()
then join that filtered array back into string
var ignores = ['a', 'b', 'c'];
var str ='abcde';
var res = str.split('').filter(function(char){
return !ignores.includes(char)
}).join('')
console.log(res)
Upvotes: 1
Reputation: 9095
you can pass the the chars to the regex pattern. See the below solution.
var string = "abcde"
var result = string.replace(/^abc+/i, '')
console.log(result)
Upvotes: 1