Javascript check link text

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

JavaScript plugin for finding links in plain-text and converting them to HTML tags.

License

Hypercontext/linkifyjs

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Читайте также:  Php exception error page

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Linkify is a JavaScript plugin. Use Linkify to find links in plain-text and convert them to HTML tags. It automatically highlights URLs, #hashtags, @mentions and more.

  • Detect URLs and email addresses
  • #hashtag, @mention and #-ticket plugins
  • React and jQuery support
  • Multi-language and emoji support
  • Custom link plugins
  • Fast, accurate and small footprint (~20kB minified, ~11kB gzipped)
  • 99% test coverage
  • Compatible with all modern browsers (Internet Explorer 11 and up)

Download the latest release for direct use in the browser, or install via NPM:

npm install linkifyjs linkify-html 

When developing in an environment with JavaScript module loader such as Webpack, use an import statement:

import * as linkify from 'linkifyjs'; import linkifyHtml from 'linkify-html';

Or in Node.js with CommonJS modules

const linkify = require('linkifyjs'); const linkifyHtml = require('linkify-html');

Note: When linkify-ing text that does not contain HTML, install and use the linkify-string package instead of linkify-html . Read more about Linkify’s interfaces.

Example 1: Convert all links to tags in the given string

const options =  defaultProtocol: 'https' >; linkifyHtml('Any links to github.com here? If not, contact test@example.com', options);

Returns the following string:

'Any links to github.com here? If not, contact test@example.com'

To modify the resulting links with a target attribute, class name and more, use the available options.

Example 2: Find all links in the given string

linkify.find('Any links to github.com here? If not, contact test@example.com');

Returns the following array

[  type: 'url', value: 'github.com', isLink: true, href: 'http://github.com', start: 13, end: 23 >,  type: 'email', value: 'test@example.com', isLink: true, href: 'mailto:test@example.com', start: 46, end: 62 > ]

Example 3: Check whether a string is a valid link:

Check if as string is a valid URL or email address:

linkify.test('github.com'); // true

Check if a string is a valid email address:

linkify.test('github.com', 'email'); // false linkify.test('noreply@github.com', 'email'); // true

Usage with React, jQuery or the browser DOM

Read the interface documentation to learn how to use linkify when working with a specific JavaScript environment such as React.

Plugins for @mentions, #hashtags and more

By default Linkify will only detect and highlight web URLs and e-mail addresses. Plugins for @mentions, #hashtags and more may be installed separately. Read the plugin documentation.

Linkify natively supports all modern browsers.

Linkify is tested on Node.js 10 and up. Older Node.js versions are unofficially supported.

View full documentation at linkify.js.org/docs

Источник

Check if URL Contains a String With JavaScript

Check if URL Contains a String With JavaScript

  1. Use indexOf() to Check if URL Contains a String
  2. Check if URL Contains a String Using a Regular Expression
  3. Use toString().includes() to Check if URL Contains a String

This article will teach you to check if a URL contains a string. We’ll do the checking using String.prototype.indexOf() , Regular Expression, and String.prototype.includes() .

Use indexOf() to Check if URL Contains a String

When a URL contains a string, you can check for the string’s existence using the indexOf method from String.prototype.indexOf() . Therefore, the argument of indexOf should be your search string.

The indexOf method works by searching for the first occurrence of that string in the URL. Meanwhile, you’ll need to use indexOf on window.location.href because it contains the URL of the current web page.

In the code below, we’ve used indexOf on window.location.href to check if the URL contains the string ‘tutorial’ .

if (window.location.href.indexOf("tutorial") > -1)   alert("The web page contains the string 'tutorial'"); > 

Check if URL Contains a String Using a Regular Expression

You can utilize a Regular Expression pattern to search if the URL contains a string. Meanwhile, you’ll need the test() method from RegExp.prototype.test() .

Since you are looking for a string in the URL, the pattern to search for should be the string itself. In the code below, we’ve used the test() method to match the string ‘html’ on DelftStack’s website.

if (/html/.test(window.location.href))   alert("The web page contains the string 'html'"); > 

Use toString().includes() to Check if URL Contains a String

A combination of toString() and the includes() method can determine if a URL contains a string. The toString() returns a string version of an object.

So, we’ll need it because we’ll get the URL from window.location , which is an object. Since we already have a string version of window.location , we can use the includes() method to determine if it contains a string.

However, the includes() method performs a case-sensitive search, and the search for hello will not match Hello . In the next code block, we’ve used the includes method and toString to determine if the URL contains the string «google» .

if (window.location.toString().includes("google"))   alert("There is 'google' in the URL"); > 

Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.

Related Article — JavaScript String

Copyright © 2023. All right reserved

Источник

Check if the URL Bar Contains a Specified or Given String in JavaScript and jQuery

www.encodedna.com

The URL bar or the Address bar of a web browser contains the address of a website. The URL also contains parameters or arguments, which we often define dynamically to communicate with elements or objects on a web page. Sometimes we need extract the data that we pass to the URL as parameters. Here I’ll show you how to check if the URL bar contains a given or specified string or text or value using JavaScript indexOf() method.

Check if a URL contains a given string or value using JavaScript

You will need this if you are working on a web site that depends on data extracted from the URL. You can extract or read URL parameters at the Server side in Asp.Net (such as using QueryString) and using JavaScript methods at the client side itself.

Whether you are using jQuery or JavaScript for your frontend, you can simply use the indexOf() method with the href property using windows.location object.

Syntax for indexOf()

The method indexOf() returns the position (a number) of the first occurrence of a given string. It will return -1 , if it finds nothing that matches the given string.

You can use the indexOf() method with href property of windows.location object. For example,

var t = window.location.href.indexOf('&book=');

If the method finds the string &book= in the URL, it will return the location (a number) and the location to the variable t .

Note: The character & indicates that I am passing an extra parameter to the URL.

I wish to check if the URL contains the parameter ?book= in jQuery. My script would be …

if (window.location.href.indexOf('?book=') > 0) < // . do something >

Here’s a complete Markup with the Script to check using jQuery .

Using JavaScript

If you are using JavaScript, you’ll simply call a function using the button’s click s event. For example

<input type="button" onclick="checkUrl()" value="Check URL" /> <script> function checkUrl() < if (window.location.href.indexOf('?book=') > 0) < console.log("The URL contains ?book"); >> </script>

The result will be the same as we saw using jQuery.

In my next post, I have shared an example explaining how to replace a given string with another string in the URL bar using JavaScript or jQuery.

Источник

Оцените статью