Jump to content

How to delete substring beginning with "#blah" until end?


pstein

Recommended Posts

Assume I want to delete a substring which starts with "#blah" until the end of string. #blah itself should be deleted too.

How can I achieve this? e.g.

var address = document.URL.replace("#blah","");

deletes only the keyword but not the following, remaining part.

If "#blah" is not found in string then nothing should happen.

 

 

Link to comment
Share on other sites

Find the position of the substring, then get everything up to that position.

let pos = document.URL.indexOf("#blah");
let address = document.URL;
if(pos >= 0) {
  address = address.substr(0, pos);
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...