How to find if a text contains url string
function replaceURLWithHTMLLinks(text)
{
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp,"<a href='$1'>$1</a>");
}
Check if text contains URL
You can use stristr()
$has_link = stristr($string, 'http://') ?: stristr($string, 'https://');
http://php.net/manual/en/function.stristr.php
Or preg_match()
preg_match('/(http|ftp|mailto)/', $string, $matches);
var_dump($matches);
https://www.php.net/manual/en/function.preg-match.php
check if string contains url anywhere in string using javascript
Try:
if(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?").test(status_text)) {
alert("url inside");
}
How to detect if String contains url and make url clickable using javascript react js
Assuming the urlify
function is working as you expect, the result should be considered an HTML string. Use dangerouslySetInnerHTML
to set the inner text of the p
tag.
Example:
<p dangerouslySetInnerHTML={{ __html: urlify(text) }} />
As the name suggests, this is potentially a dangerous function, but if you've full control over the content and computing the HTML tags that are generated, it's probably pretty safe.
If you've any concern over the content though then I suggest also using DOMPurify to help sanitize the text
value before you process it to create the links.
How to check if string contains url anywhere in string using javascript and convert it to anchor tag
I am not sure if i understand you correctly, but do you want to have it changed live or after the form was sent? If the latter, i would try something like this:
var urlCheck = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");
if(urlCheck.test(status)) {
alert("url inside");
console.log(urlCheck.exec(status)[0]);
// Here my possible solution (not tried out)
$('#status').val('<a href="http://'+urlCheck.exec(status)[0]+"' target='_blank'>the link</a>");
}
But this would also mean that you could/must check with a RegEX if the user entered http or not.
What's the best way to check if a String contains a URL in Java/Android?
Best way would be to use regular expression, something like below:
public static final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
Pattern p = Pattern.compile(URL_REGEX);
Matcher m = p.matcher("example.com");//replace with string to compare
if(m.find()) {
System.out.println("String contains URL");
}
Detect URLs in text with JavaScript
First you need a good regex that matches urls. This is hard to do. See here, here and here:
...almost anything is a valid URL. There
are some punctuation rules for
splitting it up. Absent any
punctuation, you still have a valid
URL.Check the RFC carefully and see if you
can construct an "invalid" URL. The
rules are very flexible.For example
:::::
is a valid URL.
The path is":::::"
. A pretty
stupid filename, but a valid filename.Also,
/////
is a valid URL. The
netloc ("hostname") is""
. The path
is"///"
. Again, stupid. Also
valid. This URL normalizes to"///"
which is the equivalent.Something like
"bad://///worse/////"
is perfectly valid. Dumb but valid.
Anyway, this answer is not meant to give you the best regex but rather a proof of how to do the string wrapping inside the text, with JavaScript.
OK so lets just use this one: /(https?:\/\/[^\s]+)/g
Again, this is a bad regex. It will have many false positives. However it's good enough for this example.
function urlify(text) { var urlRegex = /(https?:\/\/[^\s]+)/g; return text.replace(urlRegex, function(url) { return '<a href="' + url + '">' + url + '</a>'; }) // or alternatively // return text.replace(urlRegex, '<a href="$1">$1</a>')}
var text = 'Find me at http://www.example.com and also at http://stackoverflow.com';var html = urlify(text);
console.log(html)
Check if a link on the page contains a string
Here's an example of doing what you need. I've added 3 links in HTML and added some Javascript to find the one with the text stack
in it and click it.
var links = document.getElementsByTagName("a");
for (var i = 0, l = links.length; i < l; i++) { var link = links[i]; if (link.href.indexOf("stack") !== -1) { link.click(); }}
<a href="https://www.google.com">google</a><br/><a href="https://www.stackoverflow.com">stack overflow</a><br/><a href="https://bbc.co.uk">bbc</a>
Related Topics
Angular - How to Get Result of a Json Object
Javascript Generate Video Thumbnail from Video Url
React Component Not Re-Rendering When State Changes
How to Refresh Select Option List After Ajax Response
Keep Order of Objects Inside a Json String After They Are Parsed
Disable Prev Control on First Slide and Disable Next Control on Last Slide
How to Show an Alert After Reloading the Page in JavaScript
Check If an Array Is Empty in React Native
Failed to Execute Query Selector on Document, Id Is Not a Valid Selector
Force Facebook to Open Link in External Browser for Smart Phones
React-Native React-Navigation Undefined Is Not an Object (Evaluating 'S.Value.Startswith')
Html5 and JavaScript to Play Videos Only When Visible
How to Get the Index from a Json Object With Value
How to Clear All Input Fields in Bootstrap Modal When Clicking Data-Dismiss Button
Write a Auto-Fill and Submit Web Form Program
How to Reduce Vendor/Script Bundle.Js in Angular 5
How to Push Array to Json Object in Angular
Css Div Height Not Expanding to Fit Content or Wrapping Content