Skip to content

Instantly share code, notes, and snippets.

@phuochau
Created July 30, 2018 02:08
Show Gist options
  • Select an option

  • Save phuochau/e42f28bd77df26d55532aeca12dacac1 to your computer and use it in GitHub Desktop.

Select an option

Save phuochau/e42f28bd77df26d55532aeca12dacac1 to your computer and use it in GitHub Desktop.
[JS] Detect links and replace link by another string
export const hasLinks = (str) => {
return str.test(/(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?/gi)
}
export const groupLinkIntoAngleBracket = (str, replacedString) => {
return str.replace(/(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?/gi, replacedString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment