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

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -