Regex to conditionally replace Twitter hashtags with hyperlinks
(^|\s)#(\w*[a-zA-Z_]+\w*)
PHP$strTweet = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1#<a href="http://twitter.com/search?q=%23\2">\2</a>', $strTweet);
This regular expression says a # followed by 0 or more characters [a-zA-Z0-9_], followed by an alphabetic character or an underscore (1 or more), followed by 0 or more word characters.http://rubular.com/r/opNX6qC4sG <- test it here.
not autolinking all-numeric twitter hashtags in perl?
Your regexp wouldn't capture anchors that contain more than one letter separated by numbers, e.g. #a0a:
my @anchors = ($tweet =~ m/#(\w+)/g);
foreach my $anchor (@anchors)
{
next unless $anchor =~ m/[a-z]/i;
$tweet =~ s{#$anchor}{<a href="http://twitter.com/search?q=%23$anchor">#$anchor</a>}g;
}
e.g. consider my $tweet = "hello #123 hello #abc1a hello #a0a";
Your code produces hello #123 hello <a href="http://twitter.com/search?q=%23abc1">#abc1</a>a hello <a href="http://twitter.com/search?q=%23a9">#a0</a>a
and mine produces hello #123 hello <a href="http://twitter.com/search?q=%23abc1a">#abc1a</a> hello <a href="http://twitter.com/search?q=%23a9a">#a0a</a>
Improving a regex for the hashtag to generate a link
Adding a third group is easy:
(^|\s)(#(\w+))
|__1_|| | ||
| |_3_||
|____2_|
Replace with$1<a href="/hash/$3" class='hash_tag'>$2</a>
See proof. Saving Twitter Like Hashtags with Regex Rails
Your first regex works totally, but you must use scan
instead of split
, so your code to assign the tags would be:
def tag_list=(names)
self.tags = names.scan(/\B#\w+/).map do |tag|
Tag.find_or_initialize_by(name: tag.remove('#'))
end
save!
end
The changes are:- Use
scan
- Use
find_or_initialize_by
instead ofwhere
thenfirst_or_create!
- Use
save!
at the end to save once - You may not need
tag.remove('#')
if you want to save the hashtag with#
prefix
Replace hashtags in a single pass with regex
Here is a way to do it with a little hack:
String str = "#This is a #foo_bar #document about #nothing_but_tags!";
String res = str.replaceAll(" ?#|(?<=#\\w{0,100})_", " ").trim();
It would break with hashtags longer than 100 characters, and it would insert a space in place of hash in the tag if it happens to be the first thing in a string (hence a call to trim()
).Demo.
The 100 character limitation comes from {0,100}
portion of lookbehind. This is a limitation of Java regex engine: unlike some other regex engines, it requires the lengths of look-aneads and look-behinds to have an explicit upper limit.
How do I replace all hashtags in a contenteditable div with a hyperlink while ignoring previously replaced hashtags?
This regex looks for all hashtags that are not already wrapped in a hyperlink. It also creates two groups that we can use to form the link of the hashtag.
https://regex101.com/r/EUNySg/13
/(?!\s*#\w+\s*<\/a>)(#(\w+))/g
JQuery$(document).on("keyup", ".post-input-field", function (event) {
if (event.keyCode === 32 || event.keyCode === 13) {
let html = $(this).html();
html = html.replace( /(?!\s*#\w+\s*<\/a>)(#(\w+))/g, "<a class='hashtag' href='https://google.com/$1'>$2</a> ").replace("<br>", "");
$(this).html(html);
}
});
Below you can see that all #hashtag get highlighed, but the #notahashtag do not get highlighted. Regex for twitter hastags not linking properly?
Put the #
symbol within the <a>
tag as you did, but also take out the one preceding the tag...
Related Topics
Setting PHP Enviromental Variable While Running Command Line Script
How to Use a PHP Class from Another File
Laravel Catch Eloquent "Unique" Field Error
How to Get the Execution Time of a MySQL Query from PHP
Denormalize Nested Structure in Objects with Symfony 2 Serializer
Object Copy Versus Clone in PHP
Paypal Ipn Bad Request 400 Error
How to Use PHPize After Update to MACos Mojave
Exploding by Array of Delimiters
PHP Error: Cannot Use Object of Type Stdclass as Array (Array and Object Issues)
Optimizing MySQL Fulltext Search
How to Refresh Select2 Dropdown Menu After Ajax Loading Different Content
Php: How to See "Invisible" Characters Like \N
Mysqli_Store_Result() VS. MySQLi_Use_Result()