OK , there are many solution to this
first what causes your problem ?
in your code you just remove the class that should be page*
and that's not a regular expression because you're using the double quotes .
also classList
is a DOMTokenList which is a collection , so we need to iterate over it to test each value in the list to see if it meets specific criteria
how to solve this problem ?
first solution
use startWith string method
how ?
- iterate over each class in classList check if it starts with what
you search for remove it if yes
example
const p = document.getElementById('p');
p.classList.forEach(item=>{
if(item.startsWith('page')) {
p.classList.remove(item) ;
}
})
.page-blue {
color: blue;
}
.c {
text-align: center;
font-size: 1.4rem;
}
<p id="p" class=" c page-blue">
hello I am p
</p>
also you can use simple regular expression for that
how ?
using special-caret that tests if the input starts with the specified value
example
const p = document.getElementById('p');
p.classList.forEach(item=>{
if(/^page/.test(item)) {
p.classList.remove(item) ;
}
})
.page-blue {
color: blue;
}
.c {
text-align: center;
font-size: 1.4rem;
}
<p id="p" class=" c page-blue">
hello I am p
</p>