Script for tagging email addresses

Jeffrey Massung massung at gmail.com
Sat Mar 27 00:09:52 EDT 2010


On Mar 26, 2010, at 10:40 PM, Sivakatirswami wrote:

> anyone have an efficient script for tagging email address with links?


I have used the algorithm below several time for similar needs. The basic concept is as follows:

* Create a regular expression matching what you are looking for
* Loop over the field finding matches and 
* Turning them into links

I usually do the algorithm below in a field and just set the textStyle to "link" and change the linkText of the chunk. I've modified it for you to accept input text and create a new output text string with the HTML tags inserted. I don't know if that's what you really want, but it should give you an idea of how to proceed regardless. Note, since I'm modifying code from my library, it is untested and from memory, but should be close.

function linkEmails pText
	local tPos
	local tStart
	local tEnd
	local tOutput
	local tRegex
	local tEmail

	put "\b([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b" into tRegex
	put 1 into tPos

	-- loop over the entire input text
	repeat until matchChunk(char tPos to -1 of pText, tRegex, tStart, tEnd) is false
		add tPos to tStart
		add tPos to tEnd

		-- first dump everything up to the email address and extract the address
		put char tPos to tStart - 1 ater tOutput
		put char tStart - 1 to tEnd -1 of pText into tEmail

		-- now put the email link
		put "<a href=" & quote & "mailto:" & tEmail & quote & ">" & tEmail & "</a>" after tOutput

		-- advance and continue
		put tEnd into tPos
	end repeat

	-- dump what's left over
	put char tPos to -1 of pText into tOutput

	return tOutput
end linkEmails




More information about the use-livecode mailing list