0

I have this string, how can I make it until .html?

string

https://www.lazada.com.ph/products/fujitsu-standard-aa-rechargeable-battery-2000mah-4pcs-i237968048-s311807989.html?spm=a2o4k.searchlist.list.47.7f1a484d4gpCFd&search=1

make it into

https://www.lazada.com.ph/products/fujitsu-standard-aa-rechargeable-battery-2000mah-4pcs-i237968048-s311807989.html
KarelG
  • 5,176
  • 4
  • 33
  • 49
Micheal
  • 81
  • 1
  • 4
  • 4
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Nov 20 '18 at 07:15
  • @CertainPerformance but I have no clue how to start? – Micheal Nov 20 '18 at 07:16
  • Check [How do I parse a URL into hostname and path in javascript?](https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript) – Mohammad Nov 20 '18 at 07:22
  • 1
    Michael, it is not appropriate to suddenly change the scope of your question. Now all of those answers are invalid. – KarelG Nov 20 '18 at 07:31
  • @KarelG ok then. – Micheal Nov 20 '18 at 07:39

4 Answers4

3

Maybe you're looking for something like this example:

var str = 'https://www.lazada.com.ph/products/fujitsu-standard-aa-rechargeable-battery-2000mah-4pcs-i237968048-s311807989.html?spm=a2o4k.searchlist.list.47.7f1a484d4gpCFd&search=1';

console.log(str.match('^.+\.html')[0]);

I've used regex with the pattern ^.+\.html to match the url. The regex means: From the beginning of the string to .html


If you want to split the url to 2 parts, you could try:

var str = 'https://www.lazada.com.ph/products/fujitsu-standard-aa-rechargeable-battery-2000mah-4pcs-i237968048-s311807989.html?spm=a2o4k.searchlist.list.47.7f1a484d4gpCFd&search=1';

var parts = str.split(/\?/);

console.log(parts[0]);
console.log('?' + parts[1]);
Tân
  • 1
  • 15
  • 56
  • 102
  • There is a reason why there is something called as [`URL API`](https://developer.mozilla.org/en-US/docs/Web/API/URL) I rather prefer to see users using this instead of that regex route because this answer is not useful if you have to include other extensions as well (eg `.php` or `.asp`). – KarelG Nov 20 '18 at 07:26
  • what I have other string around the url? then it should catch the https as prefix, also how do I replace it?, I don't want to print out the url only, I want the entire text. – Micheal Nov 20 '18 at 07:29
1

You can use String.prototype.split()

var url =  "https://www.lazada.com.ph/products/fujitsu-standard-aa-rechargeable-battery-2000mah-4pcs-i237968048-s311807989.html?spm=a2o4k.searchlist.list.47.7f1a484d4gpCFd&search=1";


console.log(url.split('?', 1)[0])
Gildas.Tambo
  • 22,173
  • 7
  • 50
  • 78
0

You can use the URL API:

var url = new URL(string);
url.search = "";
url.hash = "";
return url.href;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • nice but what is the next step? I want to replace the cleaned url back into its location. – Micheal Nov 20 '18 at 07:30
  • @Micheal What do you mean by "its location"? You had a string for the url, right? Now you have the new url string. – Bergi Nov 20 '18 at 07:32
  • sorry updated my question just now. It's not only the string, the urls are within a document string. – Micheal Nov 20 '18 at 07:38
0

This works fine according to your requirment

let url = "https://www.lazada.com.ph/products/fujitsu-standard-aa-rechargeable- 
         battery-2000mah-4pcs-i237968048-s311807989.html?spm=a2o4k.searchlist.list.47.7f1a484d4gpCFd&search=1"


url.substring(0, <nbsp> url.indexOf('.html')<nbsp> + <nbsp>5)
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
this is yash
  • 533
  • 1
  • 3
  • 15