woutr_be
woutr_be

Reputation: 9732

Detecting links in javascript

I'm currently working on a project where we want to limit the amount of characters a user can enter. So far this is working, javascript is used for front-end detection, and on the back-end the string is shortened why a user disabled js.

The only problem are links, we don't want to count links as characters as they will be shortened anyway. Now I'm looking for a way to quickly detect links, find the lenght of that link and ignore it.

The js for it is pretty simple:

var currentLength = $(parent).find('textarea').val().length;
var remaining = max - currentLength;

if(remaining <= 0) {
    $(parent).find('textarea').val($(parent).find('textarea').val().substr(0, max));
    remaining = 0;
}

Anybody that can point me in the right direction on how to do this? Maybe something with regular expressions?

Upvotes: 2

Views: 1119

Answers (1)

PiTheNumber
PiTheNumber

Reputation: 23563

var urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, '');

See Detect URLs in text with JavaScript

Upvotes: 1

Related Questions