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.
3
Answers
You can do this with a regex matching
blah#
and everything that comes after it.How about using a Regex
/#blah.*$/
will replace the matched pattern and everything afterwards.I agree with the other answers that regex is the way to go here.
If you wish you can also achieve the result using indexOf & substring.