Reputation: 9364
I'm trying to replace a string from a particular index to end
I have different string inputs with a link at the end I want to delete that link
for example I have :
Hello world ! this is my website link http://www.lo.com
I want to delete the link to get only :
Hello world ! this is my website link
Code :
Var message = "Hello world ! this is my website link http://www.lo.com";
How to do that in Javascript ?
Upvotes: 0
Views: 4335
Reputation: 867
Here's how you do this:
message = message.substring(0, message.lastIndexOf("http://www.lo.com"));
(obs: use substring
instead of substr
because substr
is deprecated now)
Or if you want something more general, like removing all hyperlinks at the end of the message:
message = message.replace(/(\s*(http|https):\/\/.*$)/,"");
Have fun.
Upvotes: 2
Reputation: 57
function removeLinks(text) {
var urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, ' '); // replacing with space
}
var text = "Hello world ! this is my website link http://www.lo.com";
var corrected = removeLinks(text);
alert(corrected);
Just call the function removeLinks() which strips off the web links
Upvotes: 1
Reputation: 8354
var str = "Hello world ! this is my website link http://www.lo.com";
var idx = str.indexOf("http://");
str = str.slice(0,idx-1);
console.log(str);
Upvotes: 2