Home > Software design >  Remove Region Number
Remove Region Number

Time:10-19

I am trying to do basic regex thing but still cannot figure it out. I need to remove the region number code and only get the number onwards. For example:

( 60)123456789 --> 0123456789

I have try using this regex expression replace(/\D /g, '') but the number 6 from the region code still there which make it 60123456789

CodePudding user response:

I would use the following find and replace, in regex mode:

Find:    ^\(\ \d \)
Replace: (empty)

Here is a working regex demo.

CodePudding user response:

If you want to keep the trailing zero(s) from the number, you can capture those in a group, and then use the group value in the replacement.

const s = "( 60)123456789";
const regex = /\(\ \d*[1-9](0*)\)/;
const m = s.replace(regex, "$1");
console.log(m);

If you want to take the digits only after the leading parenthesis parts, you can also take the trailing digits until the end of the string.

const s = "( 60)123456789";
const regex = /\d $/;
const m = s.match(regex);
if (m) console.log(m[0]);

  • Related