javascript - Regular expression to limit occurrences of specific character -
i looking aregular expression can enter limit number of times special character appears.
example: have limit *
maximum count of 5
in whole text, should stack***over*f**low
.
i tried in directive:
var transformedinput = text.replace(/[^0-9][*]{6}/g, '');
but it's not working. can please me this?
not sure why need replace, perhaps you're trying prevent anymore asterisks being entered? in case, i'd recommend expand scope bit , allow more js of heavy lifting. if want prevent characters being entered, can check length of asterisks on every keypress event.
var str = 'this* ** test string. **'; var count = (str.match(/\*/g) || []).length; if (count >= 5) { alert('doing something...') } else {alert(count);}
Comments
Post a Comment