JSFiddle - React, Tailwind, and code Playground JavaScript var str = "%PT1234!!test@@"; var res = str.replace(/[^%PT!{2}@{2}]/g, ""); alert(res)