Convert plain text email to clickable link - REGEX/jQuery - regex

I am trying to convert plain text email addresses into clickable mailto links inside of a table.
I have the following function that is converting found links into mailto links, but it only seems to work for the first found link. Any subsequent links (2nd, 3rd occurrence etc.) remain as plain text links. I can't seem to figure out what I might be doing wrong. Any help would be much appreciated!
The code:
HTML
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<div class='filter-email-box'>
<div>This is a sample text which contains john#gmail.com </div>
<div>This text contains two emails adam#example.com and paul#example.com </div>
</div>
Javascript
$(".filter-email-box div").filter(function(){
var html = $(this).html();
var emailPattern = /[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
var matched_str = $(this).html().match(emailPattern);
if(matched_str){
$(this).html(html.replace(emailPattern,"<a href='mailto:"+matched_str+"'>"+matched_str+"</a>"));
return $(this)
}
})
Heres the fiddle I've setup:
http://jsfiddle.net/z6LF5/

When there's more than one email address, JQuery method match returns an array (when the global search flag g is set), so we're looping over that array (called matched_str in this case) and replacing the matching emails.
$(".filter-email-box div").filter(function () {
var html = $(this).html();
var emailPattern = /[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/g;
var matched_str = $(this).html().match(emailPattern);
if ( matched_str ) {
var text = $(this).html();
$.each(matched_str, function (index, value) {
text = text.replace(value,"<a href='mailto:"+value+"'>"+value+"</a>");
});
$(this).html(text);
return $(this)
}
})
JSFiddle

Use the g (global) modifier which ( finds all matches rather than stopping after the first match ).
You could implement the following:
$('.filter-email-box div').ready(function() {
$('.filter-email-box div').each(function() {
var html = $(this).html();
var regex = /([a-z0-9._-]+#[a-z0-9.-]+\.[a-z]{2,4})/ig
var text = html.replace(regex, "<a href='mailto:$1'>$1</a>");
$(this).html(text);
});
});
Fiddle

if anyone is interested I translated the jquery code to vanilla JS code
var elem = document.querySelector('.filter-email-box');
var html = elem.innerHTML;
if (html) {
var regex = /([a-z0-9._-]+#[a-z0-9.-]+\.[a-z]{2,4})/ig;
elem.innerHTML = html.replace(regex, '$1');
}
Demo link: https://jsfiddle.net/q0x4tjr3/

Related

search and replace on google slide refuse to work

I'm trying to replace placeholder Text with text from form or var on Google Slides using Google Script
everything looks like it working but when I open the new google slides the placeholder are not changed. Thanks.
function project(){
var projectname = '666'
var category = '12345'
var subtitle = '12345'
var excerpt = '12345'
//PP-Slides template file, and you get it by ID
var file = DriveApp.getFileById('fileID');
//We can make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
var folder = DriveApp.getFolderById('folderid')
var copy = file.makeCopy(projectname + '-' + "Project", folder).getId();
var Slides = SlidesApp.openById(copy).getSlides();
Slides.forEach(slide => requests = [{
replaceAllText: {
containsText: {
text: '{{subtitle}}',
matchCase: true
},
replaceText: projectname
}
}]);
Logger.log(copy);
Logger.log(Slides);
console.log(projectname);
}

Laravel 5.8 parsing HTML text from database to modal

I'm trying to show HTML text which is an event program stored in database from TinyMCE textarea in a bootstrap modal.
I need to parse it before, for that I found that Laravel 5.* uses:{!! !!} to parse but with $event->program it always shows the last one stored.
here is my js code
$('#eventDetails').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var id = button.data('id')
var name = button.data('name')
var description = button.data('description')
var program = button.data('program')
var date = button.data('date')
var modal = $(this)
modal.find('.modal-body #id').text(id);
modal.find('.modal-body #name').text(name);
modal.find('.modal-body #description').text(description);
modal.find('.modal-body #program').text(program);
modal.find('.modal-body #date').text(date);
});
so what shoud I do please !!?

Color rows when "group by" sharepoint 2013

I need some help. I'm trying to set color when group by in a sharepoint list.
I'm using the following code
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
OnPostRender: function(ctx) {
var statusColors = {
'Liberada' : '#FFF1AD',
'Cancelada' : '#FFD800',
'Créditos' : '#01DF3A'
};
var rows = ctx.ListData.Row;
for (var i=0;i<rows.length;i++)
{
var status = rows[i]["Status"];
var rowId = GenerateIIDForListItem(ctx, rows[i]);
var row = document.getElementById(rowId);
row.style.backgroundColor = statusColors[status];
}
}
});
});
Can someone give a hint?
As I can see in my local Sharepoint 2013 environment, when I create a view with a group by, Sharepoint add a CSS class named « ms-gb » on the table cell that contains the group name.
Knowing this, a solution would be to edit the page of the view.
Then you can add to the page a Script Editor Webpart.
Inside this Script Editor Webpart you can add some JavaScript code to add the background-color.
Something like this for example :
<script>
$(document).ready(function(){
var statusNames = ['Liberada', 'Cancelada', 'Créditos'];
var statusColors = {
'Liberada' : '#FFF1AD',
'Cancelada' : '#FFD800',
'Créditos' : '#01DF3A'
};
$('td.ms-gb').each(function(){
for (var x=0;x<statusNames.length;x++) {
if ($(this).text().indexOf(statusNames[x]) > -1) {
$(this).css('background-color',statusColors[statusNames[x]]);
}
}
});
});
</script>
If you try this script and you don't already have a reference to JQuery in your masterpage or page layout, you will need to add one inside the script editor webpart.
You can download a local copy of JQuery or use one hosted like :
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Hope this help.

Force update array on Repeater

When using XML to build an repeater, like the example above, how to update the array later?
<Repeater items="{{ letras }}" id="rep">
<Repeater.itemsLayout>
<StackLayout horizontalAlignment="center" orientation="horizontal" />
</Repeater.itemsLayout>
<Repeater.itemTemplate>
<Button text="{{ l }}" cssClass="palavraPrincipal" />
</Repeater.itemTemplate>
</Repeater>
I know that there is an way to do that if I create the repeater on Javascript file, but on a pre-made XML repeater?
To make changes in the viewmodel (the letras array in your example) reflect in the UI you need to use Native Script's data binding.
Below is an example which, given the XML view in your post, will:
Require the 2 modules, observable (for observable objects) and observable-array (for arrays).
Create a viewmodel, which is the object which holds the data
Bind the viewmodel to your page's binding context.
Every 1 second, add a new item to the viewmodel (which will get reflected to the UI).
I set the binding context of the page to the viewmodel. There's no need to set the binding context on the Repeater element, as calebeaires do in his example, as the Repeater will look at the page's binding context.
However, if you want to, you can use e.g. 2 different binding contexts, 1 for the page and one for the Repeater element. Just substitute
page.bindingContext = viewModel;
for
page.getViewById('rep').bindingContext = viewModel;
Full code:
var observableModule = require("data/observable");
var observableArrayModule = require("data/observable-array");
var viewModel = new observableModule.Observable({
letras: new observableArrayModule.ObservableArray([
{ l: 'First' },
{ l: 'Second' },
{ l: 'Third' },
])
});
function pageLoaded(args) {
var page = args.object;
page.bindingContext = viewModel;
//page.getViewById('rep').bindingContext = viewModel;
// Add a new item every 1 second.
setInterval(function() {
viewModel.letras.push({
l: 'New item'
});
}, 1000);
}
exports.pageLoaded = pageLoaded;
Find out:
A. Declare and variable:
var repeater;
B. Declare an ID to the repeater:
<Repeater items="{{ items }}" id="rep">
<Repeater.itemTemplate>
<Button text="{{ $value }}" />
</Repeater.itemTemplate>
</Repeater>
C. Redeclare it on page load:
function pageLoaded(args) {
var page = args.object;
repeater = view.getViewById(page, "rep");
page.bindingContext = jogo;
}

how to set alt or title tag for ImageSurface?

The image generated using the following example Famo.us example: ImageSurface generates an <img> tag without alt or title attributes.
Is there a built-in function that allows adding HTML attributes?
There are currently no built in functions but by looking at the source code I figured out a way to do such a thing. Keep in mind though, I think the better alternative would be to just use a Surface and inject and img tag with all the attributes you want into the content attribute..
Here is what I had to do using the ImageSurface. Note that the function is in a Timeout, and this is only because the image tag needed time to load for me to access it via the Surface..
var Engine = require("famous/core/Engine");
var Modifier = require("famous/core/Modifier");
var ImageSurface = require("famous/surfaces/ImageSurface");
var Timer = require("famous/utilities/Timer")
var mainCtx = Engine.createContext();
var image = new ImageSurface({
size: [200, 200],
});
image.setContent("content/famous_symbol.svg");
mainCtx.add(new Modifier({origin: [.5, .5]})).add(image);
add_title_alt = function(){
if (image._currTarget) {
image._currTarget.alt = "my-alt";
image._currTarget.title = "my-title";
} else {
Timer.setTimeout(add_title_alt,100);
}
}
Timer.setTimeout(add_title_alt,100);
Again though, to make things less sticky.. I would just do something like this..
image = new Surface({
content:"<img alt='my-alt' title='my-title' src='content/famous_symbol.svg' />"
})
Hope this helps!