skip to Main Content

I have a string "perm=delete;search;update;view-shielded;roles=Admin;CustomerService" and want to convert it as "perm=delete;search;update;view-shielded/roles=Admin;CustomerService".
I try regex but no luck. Please help me with regex.

2

Answers


  1. You can use this one liner to seprate individual parts:

    const str ='perm=delete;search;update;view-shielded;roles=Admin;CustomerService'
    str.split(';').reduce((acc, val) => (val.includes('=') ? acc.push(val + ';') : acc[acc.length - 1] += (val + ';'), acc), []);
    
    // ["perm=delete;search;update;view-shielded;","roles=Admin;CustomerService;"]
    
    Login or Signup to reply.
  2. You could try a regex. Here you replace ; followed by a word and =:

    const str = 'perm=delete;search;update;view-shielded;roles=Admin;CustomerService';
    
    const result = str.replace(/;(?=w+=)/g, '/');
    
    console.log(JSON.stringify(result));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search