0

I'm trying to find characters when preceded with exact string but not the preceding string. how to do that?

I have the string

+1545464454<+440545545454<+210544455454<+75455454545

the above string are phone numbers with international prefix, but some of them have 0 between prefix and number and I need to take it out.

I have /(\+4|\+44|\+7 ... allprefixes here...)0/g but this selects the prefix as well and I need to select only 0

I's in the javascript

webduvet
  • 4,220
  • 2
  • 28
  • 39

2 Answers2

3

You're almost close to that. Just use Capturing groups and replace function like below. Most languages support capturing groups.

/(\+(?:4|44|7 ... allprefixes here without `+`...))0/g

REplacement string:

$1

or \1

If you're on PHP, \K should work. \K discards previously matched characters from printing at the final.

'~\+(?:4|44|7)\K0~g'

In javascript.

> var str = "+1545464454<+440545545454<+210544455454<+75455454545"
> str.replace(/(\+(?:44|21|7|4))0/g, "$1")
'+1545464454<+44545545454<+21544455454<+75455454545'
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
2

If your language supports lookbehinds, you can use it as used in the following regex

/(?<=\+(4|44|7))0/g

Javascript doesn't support it. So you'll need to use something like this

str.replace(/(\+(4|44|7))0/, "$1");
Amit Joki
  • 58,320
  • 7
  • 77
  • 95