Duru Can Celasun
Duru Can Celasun

Reputation: 1681

Javascript to linkify string in "minutes:seconds" format

I have a video site where people can comment under each video.

I'm trying to create a linkify function that will parse a user's comment and look for substrings in the "minutes:seconds" format. The link will have a click event that will trigger a function call, which in turn will seek the video to that particular moment.

Any help is appreciated.

Upvotes: 0

Views: 137

Answers (1)

jabclab
jabclab

Reputation: 15052

This should help get you started (you'll have to tweak the regular expression to validate the time correctly):

function linkify(str) {
    return str.replace(/(\d{2}):(\d{2})/, "<span onclick='video($1, $2)'>$1:$2</span>");
}

function video(mins, seconds) {
    mins = window.parseInt(mins, 10);
    seconds = window.parseInt(seconds, 10);   

    // Do the stuff with the video
}

console.log(linkify("Look at the video at 01:23 for something interesting"));

Upvotes: 1

Related Questions