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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -