Reputation: 1145
I'm trying to use classList.replace()
with regular expression. My goal is replacing an expression like badge-something with an other value like badge-success.
I've tried this:
element.classList.replace(/badge-*/i, 'badge-success')
But it returns false and doesn't change nothing. What am I missing?
Upvotes: 3
Views: 1915
Reputation: 51
classList.replace took string as an argument, so i think that is why it is not working. But you can achieve your goal by twisting your code little bit, repeat these steps
. Thank you
Upvotes: 0
Reputation: 85767
Element.classList
is a DOMTokenList
(not a string).
DOMTokenList.replace
takes two strings, not a regex. The first argument is the exact class name you want to replace. Patterns are not supported.
If you want to use regexes, you need a string:
element.className = element.className.replace(/(^|\s)badge-\S+/g, '$1badge-success');
Upvotes: 6