skip to Main Content
let updateStreamSelectedCourse = 'mech (electronics)'.replace(/([^w]+)/g, '_', );
console.log(updateStreamSelectedCourse);

output should be mech_electronics

but now im geting output is mech_electronic_

2

Answers


  1. You can’t replace them with different characters, but You can replace the last _ with the following

    let updateStreamSelectedCourse = 'mech (electronics)'.replace( /([^w]+)/g, '_', ).replace(/_$/,"");
    console.log(updateStreamSelectedCourse)
    Login or Signup to reply.
  2. /_$/g,'' should be what you need :

    let updateStreamSelectedCourse = 'mech (electronics)'.replace( /([^w]+)/g, '_', ).replace(/_$/g,'');
    console.log(updateStreamSelectedCourse)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search