0

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 ?

Starscream1984
  • 3,072
  • 1
  • 19
  • 27
Stranger B.
  • 9,004
  • 21
  • 71
  • 108
  • Try to explore the power of regular expressions https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions . – Stepashka Nov 16 '15 at 14:14
  • 1
    Possible duplicate of [Regex in Javascript to remove links](http://stackoverflow.com/questions/960156/regex-in-javascript-to-remove-links) – Mihai Matei Nov 16 '15 at 14:16
  • To clarify, you want the (text you have presented + the link address) to show only and the link itself to be disabled? – Mike Horstmann Nov 16 '15 at 14:16
  • @MikeHorstmann I think the question is very clear.. there is no disabled link needed as you can see in the wanted result.. – Mihai Matei Nov 16 '15 at 14:17
  • Have a look at `indexOf` and `substr` or `substring` – Pete Nov 16 '15 at 14:18
  • @MateiMihai If the question was that clear, why would the OP not just change the variable to exclude the website link? – Mike Horstmann Nov 16 '15 at 14:26
  • @MikeHorstmann are you serious with that question? That text might come from an user input.. – Mihai Matei Nov 16 '15 at 14:30
  • @MateiMihai Exactly, I don't know but thank you though for answering the question to the O.P. that was super helpful for me. Please, stop turning Stack into a "You're wrong, your train of thought is wrong" type of community. We're supposed to be here to help each other not be condescending. I could REALLY be condescending with a lot of the outsourced code I have to spend my entire day rewriting that was written assumption driven people like you. Make helpful comments or just don't respond to people who didn't post the question your kind are ruining Stack. – Mike Horstmann Nov 16 '15 at 14:42

3 Answers3

2
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);
Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
1
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

puppy
  • 57
  • 1
  • 1
  • 6
1

Here's how you do this:

message = message.substr(0, message.lastIndexOf("http://www.lo.com"));

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.

idancali
  • 847
  • 5
  • 10