JavaScript - Replace variable from string in all occurrences -


ok, know if have character '-' , want remove in places in string javascript, ...

someword = someword.replace(/-/g, ''); 

but, when applying array of characters, s not working ...

  const badchars = ('\/:*?"<>|').split('');   let filename = title.replace(/ /g, '-').tolocalelowercase();   (let item = 0; item < badchars.length; item++) {     // below not work global '/ /g'     filename = filename.replace(/badchars[item]/g, '');   } 

any ideas?

/badchars[item]/g looks badchars, literally, followed i, t, e, or m.

if you're trying use character badchars[item], you'll need use regexp constructor, , you'll need escape regex-specific characters.

escaping regular expression has been well-covered. using that:

filename = filename.replace(new regexp(regexp.quote(badchars[item]), 'g'), ''); 

but, don't want that. want character class:

let filename = title.replace(/[\/:*?"<>|]/g, '-').tolocalelowercase(); 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -