replace in javascript to cut off part of string
up vote
0
down vote
favorite
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
javascript ecmascript-6
add a comment |
up vote
0
down vote
favorite
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
javascript ecmascript-6
4
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, 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 Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 20 at 7:15
@CertainPerformance but I have no clue how to start?
– Micheal
Nov 20 at 7:16
Check How do I parse a URL into hostname and path in javascript?
– Mohammad
Nov 20 at 7: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 at 7:31
@KarelG ok then.
– Micheal
Nov 20 at 7:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
javascript ecmascript-6
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
javascript ecmascript-6
javascript ecmascript-6
edited Nov 20 at 7:37
KarelG
3,97632443
3,97632443
asked Nov 20 at 7:15
Micheal
213
213
4
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, 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 Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 20 at 7:15
@CertainPerformance but I have no clue how to start?
– Micheal
Nov 20 at 7:16
Check How do I parse a URL into hostname and path in javascript?
– Mohammad
Nov 20 at 7: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 at 7:31
@KarelG ok then.
– Micheal
Nov 20 at 7:39
add a comment |
4
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, 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 Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 20 at 7:15
@CertainPerformance but I have no clue how to start?
– Micheal
Nov 20 at 7:16
Check How do I parse a URL into hostname and path in javascript?
– Mohammad
Nov 20 at 7: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 at 7:31
@KarelG ok then.
– Micheal
Nov 20 at 7:39
4
4
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, 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 Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 20 at 7:15
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, 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 Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 20 at 7:15
@CertainPerformance but I have no clue how to start?
– Micheal
Nov 20 at 7:16
@CertainPerformance but I have no clue how to start?
– Micheal
Nov 20 at 7:16
Check How do I parse a URL into hostname and path in javascript?
– Mohammad
Nov 20 at 7:22
Check How do I parse a URL into hostname and path in javascript?
– Mohammad
Nov 20 at 7:22
1
1
Michael, it is not appropriate to suddenly change the scope of your question. Now all of those answers are invalid.
– KarelG
Nov 20 at 7:31
Michael, it is not appropriate to suddenly change the scope of your question. Now all of those answers are invalid.
– KarelG
Nov 20 at 7:31
@KarelG ok then.
– Micheal
Nov 20 at 7:39
@KarelG ok then.
– Micheal
Nov 20 at 7:39
add a comment |
4 Answers
4
active
oldest
votes
up vote
3
down vote
accepted
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]);
There is a reason why there is something called asURL API
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 at 7: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 at 7:29
add a comment |
up vote
1
down vote
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])
add a comment |
up vote
0
down vote
You can use the URL
API:
var url = new URL(string);
url.search = "";
url.hash = "";
return url.href;
nice but what is the next step? I want to replace the cleaned url back into its location.
– Micheal
Nov 20 at 7: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 at 7:32
sorry updated my question just now. It's not only the string, the urls are within a document string.
– Micheal
Nov 20 at 7:38
add a comment |
up vote
0
down vote
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)
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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]);
There is a reason why there is something called asURL API
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 at 7: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 at 7:29
add a comment |
up vote
3
down vote
accepted
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]);
There is a reason why there is something called asURL API
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 at 7: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 at 7:29
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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]);
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]);
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]);
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]);
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]);
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]);
edited Nov 20 at 7:36
answered Nov 20 at 7:19
Foo
1
1
There is a reason why there is something called asURL API
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 at 7: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 at 7:29
add a comment |
There is a reason why there is something called asURL API
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 at 7: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 at 7:29
There is a reason why there is something called as
URL API
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 at 7:26
There is a reason why there is something called as
URL API
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 at 7: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 at 7:29
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 at 7:29
add a comment |
up vote
1
down vote
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])
add a comment |
up vote
1
down vote
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])
add a comment |
up vote
1
down vote
up vote
1
down vote
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])
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])
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])
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])
answered Nov 20 at 7:26
Gildas.Tambo
16.3k33663
16.3k33663
add a comment |
add a comment |
up vote
0
down vote
You can use the URL
API:
var url = new URL(string);
url.search = "";
url.hash = "";
return url.href;
nice but what is the next step? I want to replace the cleaned url back into its location.
– Micheal
Nov 20 at 7: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 at 7:32
sorry updated my question just now. It's not only the string, the urls are within a document string.
– Micheal
Nov 20 at 7:38
add a comment |
up vote
0
down vote
You can use the URL
API:
var url = new URL(string);
url.search = "";
url.hash = "";
return url.href;
nice but what is the next step? I want to replace the cleaned url back into its location.
– Micheal
Nov 20 at 7: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 at 7:32
sorry updated my question just now. It's not only the string, the urls are within a document string.
– Micheal
Nov 20 at 7:38
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use the URL
API:
var url = new URL(string);
url.search = "";
url.hash = "";
return url.href;
You can use the URL
API:
var url = new URL(string);
url.search = "";
url.hash = "";
return url.href;
answered Nov 20 at 7:24
Bergi
360k56535855
360k56535855
nice but what is the next step? I want to replace the cleaned url back into its location.
– Micheal
Nov 20 at 7: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 at 7:32
sorry updated my question just now. It's not only the string, the urls are within a document string.
– Micheal
Nov 20 at 7:38
add a comment |
nice but what is the next step? I want to replace the cleaned url back into its location.
– Micheal
Nov 20 at 7: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 at 7:32
sorry updated my question just now. It's not only the string, the urls are within a document string.
– Micheal
Nov 20 at 7:38
nice but what is the next step? I want to replace the cleaned url back into its location.
– Micheal
Nov 20 at 7:30
nice but what is the next step? I want to replace the cleaned url back into its location.
– Micheal
Nov 20 at 7: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 at 7:32
@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 at 7:32
sorry updated my question just now. It's not only the string, the urls are within a document string.
– Micheal
Nov 20 at 7:38
sorry updated my question just now. It's not only the string, the urls are within a document string.
– Micheal
Nov 20 at 7:38
add a comment |
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
up vote
0
down vote
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)
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)
edited Nov 20 at 7:37
chŝdk
23.2k52852
23.2k52852
answered Nov 20 at 7:29
this is yash
767
767
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387991%2freplace-in-javascript-to-cut-off-part-of-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, 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 Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 20 at 7:15
@CertainPerformance but I have no clue how to start?
– Micheal
Nov 20 at 7:16
Check How do I parse a URL into hostname and path in javascript?
– Mohammad
Nov 20 at 7: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 at 7:31
@KarelG ok then.
– Micheal
Nov 20 at 7:39