Emily Virginita
Emily Virginita

Reputation: 161

How to not match/capture characters needed to target something? Regular Expressions

Let's assume regexp

/www\.([^\.]+)/ig

Here string like www.test.com with substition to google will result in google.com instead of www.google.com because we captured www. as well.

www. was needed to target the url but I don't wanna actually capture it. I needed this characters to track down my string but I don't want them to be matched. How to do this thing?

Without grouping, but by just matching and substituting match

Upvotes: 0

Views: 22

Answers (1)

marvel308
marvel308

Reputation: 10458

You can use the regex

(www\.)([^\.]+)

and replace it with the first group + google i.e www.google

see the regex101 demo.

To do this without grouping you can use a positive lookbehind

(?<=www\.)([^\.]+)

see the regex101 demo

Upvotes: 1

Related Questions