skip to Main Content

we have a string like this:

const a = `3. n Trust me - I know about these thins.`;

the 3. n part can be any number. and we want to remove it from the beginning of the string so one way is simply doing this:

const a = `3. n Trust me - I know about these thins.`;

console.log(a.replace('3. n', '').trim())

As you see the issue is using this approach we need to change the number each time to remove it from the sting.

The desired result would be:

"Trust me - I know about these thins."

How would you do this?

2

Answers


  1. You can replace the string with /^d+.s*/.

    Where:

    ^ asserts the start of the string.

    d+ matches one or more digits.

    . matches the period character.

    s* matches zero or more whitespace characters.

    Demo:

    const a = `3. n Trust me - I know about these things.`;
    const result = a.replace(/^d+.s*/, '');
    console.log(result); //Trust me - I know about these things.
    Login or Signup to reply.
  2. You can try this regex /^[0-9]+.s+/

    const a = `3. n Trust me - I know about these thins.`;
    
    console.log(a.replace(/^[0-9]+.s+/, '').trim())
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search