what's the regex for removing everything after the last / if it starts with this

Can you help me with the regex for deleting everything at the end of a string, if the end of the string start with ?_t_id=

ie:

domain.it/antioxidanter/?_t_id=187
domain.it/animaliska/?_t_id=211

Upvotes: 0

Views: 37

Answers (2)

jp-jee
jp-jee

Reputation: 1523

To match the part you want to remove (replace by empty string), use

/\/\?_t_id=(?!.*\/).*/

This will match (bold):

  • domain.it/antioxidanter/?_t_id=187

To match the part you want to keep, to (re-)assign it to a variable, use

/(.+(?=\?_t_id=.*))|.+(?!\?_t_id=.*)/

This will match (bold):

  • domain.it/antioxidanter/?_t_id=187
  • domain.it/animaliska/any_id=211

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Search:

\?_t_id=\d+$

replace with:

empty string

Since ? is a special character, you need to escape that in-order to match a literal ? symbol. In js,

string.replace(/\?_t_id=\d+$/m, '');

Upvotes: 1

Related Questions