简体   繁体   中英

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

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, '');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM