Have one requirement where suppose we insert value alphanumeric ABC123 then it should output as CCCNNN(Alphabets replace by C and numbers by N)
if value is ABC 123(then CCCSNNN) spcae is replaced by N
if values i ABC then CCC
if values is suppose 1234 then NNNN
How to write a regex for the same please do let me know about the same.


Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 28, 2022, 4:15 AM EST
You can try this:
var str = "XY 123";
var str = str.replace(/[A-Z]/g, 'C').replace(/[0-9]/g, 'N').replace(/ /g, 'S');
Output: CCSNNN