0

Could you help me extract "women-watches" from the string:

https://www.aliexpress.com/category/200214036/women-watches.html?spm=2114.search0103.0.0.160b628cMC1npI&site=glo&SortType=total_tranpro_desc&g=y&needQuery=n&shipFromCountry=cn&tag=

I tried

\/(?:.(?!\/.+\.))+$

But I don't know how to do it right.

2
  • Please add the full code you are using to extract so that we can help. Commented Dec 15, 2018 at 8:55
  • If by "last occurrence" you mean the basename of the url, you should edit your question/title to reflect that. Commented Dec 15, 2018 at 9:09

1 Answer 1

1

One option could be to use a capturing group to match a word character or a hyphen. Your match will be in the first capturing group.

^.*?\/([\w-]+)\.html

That will match:

  • ^ Start of the string
  • .*? Match any character except a newline non greedy
  • \/ Match /
  • ([\w-]+) Capturing group to match 1+ times a wordcharacter of a hyphen
  • \.html Match .html

Regex demo

const regex = /^.*?\/([\w-]+)\.html/;
const str = `https://www.aliexpress.com/category/200214036/women-watches.html?spm=2114.search0103.0.0.160b628cMC1npI&site=glo&SortType=total_tranpro_desc&g=y&needQuery=n&shipFromCountry=cn&tag=`;
console.log(str.match(regex)[1]);

Another option to match from the last occurence of the forward slash could be to match a forward slash and use a negative lookahead to check if there are no more forward slashes following. Then use a capturing group to match not a dot:

\/(?!.*\/)([^.]+)\.html

Regex demo

const regex = /\/(?!.*\/)([^.]+)\.html/;
const str = `https://www.aliexpress.com/category/200214036/women-watches.html?spm=2114.search0103.0.0.160b628cMC1npI&site=glo&SortType=total_tranpro_desc&g=y&needQuery=n&shipFromCountry=cn&tag=`;
console.log(str.match(regex)[1]);

Without using a regex, you might use the dom and split:

const str = `https://www.aliexpress.com/category/200214036/women-watches.html?spm=2114.search0103.0.0.160b628cMC1npI&site=glo&SortType=total_tranpro_desc&g=y&needQuery=n&shipFromCountry=cn&tag=`;
let elm = document.createElement("a");
elm.href = str;
let part = elm.pathname.split('/').pop().split('.')[0];
console.log(part);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. But I'd like to know how to get any characters between last / and .html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.