skip to Main Content

I am trying to convert string to camelCase using first some letter(user). Example: useracting, here after user from the string next letter it should be like caps(userActing). How to do it in javascript.

var s = "useracting";

var newval = s.replace(/-./g, x=>x[1].toUpperCase());

alert(newval);//userActing

newval output should be like userActing

Demo: https://stackblitz.com/edit/js-ylpazx?file=index.js

2

Answers


  1. You can try using the callback function like the following way:

    var s = "useracting";
    
    var newval = s.replace(/user([a-z])/g, (match, group1) => 'user' + group1.toUpperCase());
    
    console.log(newval); //userActing
    Login or Signup to reply.
  2. I have another solution.

    function replacer(match,p1,offset){
        return match.toUpperCase();
    }
    let s = "useracting";
    let r=s.replace(/(?<=user)w{1}/,replacer);
    console.log(r)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search