Is there any way to use regular expression alone or with help of javascript to do the following
from
JavaScript
x
2
1
<div class="type-c red blue">
2
to
JavaScript
1
2
1
<div type="c" class="red blue">
2
Advertisement
Answer
This regular expression will match what you describe, regardless of the position of “type-xxx” in class attribute
JavaScript
1
2
1
/class="([^"]*)type-(w+)([^"]*)"/g
2
Combining with a string replace
JavaScript
1
3
1
let value = '<div class="type-a b">test</div><div class="a type-b">test 2</div>';
2
value.replace(/class="([^"]*)type-(w+)([^"]*)"/g, 'type="$2" class="$1$3"');
3
this will yield the result
JavaScript
1
2
1
<div type="a" class="b">test</div><div type="b" class="a">test 2</div>
2